Matching the character, but not including this character and everything after

Before anyone says it's a duplicate of this or that . JavaScript

hasn't look behind

, so I was struggling with this. It has look ahead

, as mentioned here

I really want to do this. I have: -
hey:blah,{'some':'obj','another':[4,5,0]}

I want to extract everything after, but not including the first one :

. So my result would be: -
blah,{'some':'obj','another':[4,5,0]}

My attempt: -
(:.+) //gives :blah,{'some':'obj','another':[4,5,0]}

- please note that it has :


[^\w:].+ //gives ,{'some':'obj','another':[4,5,0]}

- not expected result. blah

is absent.

I am trying to do it in pure regex and trying to avoid looping or any kind of string manipulation of this type.
My attempt hasn't helped yet.

+3


source to share


3 answers


^[^:]*:(.*$)

      

You can try this. Capture a group 1. Watch the demo.



https://regex101.com/r/vH0iN5/1

+2


source


Regexps

If you don't want something to be selected, you can wrap it as:

(?=:(.*))

      


If you want to go to the next colon:

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

      



JavaScript code

Since nothing is selected, it [0]

will be empty. Access [1]

:

/(?=:(.*))/.exec("hey:blah,{'some':'obj','another':[4,5,0]}")[1];
//                         Use [1] not [0]                       ^^

      

However, there is something wrong:

var str = "hey:blah,{'some':'obj','another':[4,5,0]}";
str.substr(str.indexOf(':') + 1);

      

+3


source


you can use string.replace

string.replace(/^[^:]*:/, "");

      

+2


source







All Articles