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.
source to share
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);
source to share