Regex to get the last occurence of "::" character

Viewed 642

I'm working with a third party library that concatenates a string with a double colon ::. I need to split that string because the element on the right hand side is important to me, but the left doesn't matter.

ie

This is not what I want::what-i-want

Note: the text on the right hand side does not contain any : characters

I need to write a regex to get the text on the right hand side.

string.split(/::(.*)$/)

but this doesn't work because the text on the left hand side does can contain : characters

The regex needs to work for this case

var string = ":this:should:split::correctly:::and-return-this"
3 Answers

You could split on a double colon, and assert what is on the left and right is any char except a colon.

(?<=[^:])::(?=[^:])

Regex demo

let s = ":this:should:split::correctly:::and-return-this";
console.log(s.split(/(?<=[^:])::(?=[^:])/).pop());

If you also want to split when the :: is either at the start or end of the string:

(?<!:)::(?!:)

Regex demo

You can do this using Regex or string functions:

Regex:

  • To get the text after the first occurrence of :: - use ::(.+) - this will work even if the string starts with ::
  • To get the text after the last occurrence of :: - use ^.*::(.*) - this will work for strings with : after the last ::, but it will return nothing if the last characters are ::

var string = ":this:should:split::correctly:::and-return-this";
var result = /::(.+)/.exec(string)[1];
console.log("After first occurrence = " + result);

var string = ":this:should:split::correctly:::and-return-this";
var result = /^.*::(.*)/.exec(string)[1];
console.log("After last occurrence = " + result);

var string = ":this:should:split::correctly:::and-return:this";
var result = /^.*::(.*)/.exec(string)[1];
console.log("Single : in last part = " + result);


var string = ":this:should:split::correctly:::and-return-this:";
var result = /^.*::(.*)/.exec(string)[1];
console.log("Ends with : = " + result);

var string = ":this:should:split::correctly:::and-return-this::";
var result = /^.*::(.*)/.exec(string)[1];
console.log("Ends with :: = " + result);

var string = "::this:should:split::correctly:::and-return-this";
var result = /^.*::(.*)/.exec(string)[1];
console.log("Starts with :: = " + result);

String Functions:

You can use indexOf / lastIndexOf to get the position of the :: and then slice the string from that position to the end.

indexOf documentation
lastIndexOf Dcoumentation
Slice Documentation

var string = ":this:should:split::correctly:::and-return-this";
var result = string.slice(string.indexOf('::') + 2, string.length);
console.log("After first occurrence = " + result);

result = string.slice(string.lastIndexOf('::') + 2, string.length);
console.log("After last occurrence = " + result);

var string = ":this:should:split::correctly:::and-return:this";
result = string.slice(string.lastIndexOf('::') + 2, string.length);
console.log("Single : in last part = " + result);

var string = ":this:should:split::correctly:::and-return-this:";
result = string.slice(string.lastIndexOf('::') + 2, string.length);
console.log("Ends with : = " + result);

var string = ":this:should:split::correctly:::and-return-this::";
result = string.slice(string.lastIndexOf('::') + 2, string.length);
console.log("Ends with :: = " + result);

var string = "::this:should:split::correctly:::and-return-this";
result = string.slice(string.indexOf('::') + 2, string.length);
console.log("Starts with :: = " + result);

Related