Skip to content Skip to sidebar Skip to footer

Simple Js Regex To Extract Word Between Parenthesis

I would need a regex to extract 'Fred' (identified as user) from a string like var s='city(NewYork)country(USA)user(Fred)age(30)' tried this not working s.match(/(user\()\w+/)

Solution 1:

Move the parens. Unless you're 100% certain that s is a string, you should prefer /.../.exec(string) over string.match(regex).

Demo: http://jsfiddle.net/3S6cV/

var match = s.match(/user\((\w+)\)/);
if (match) {
    match = match[1];
} else {
    match = "Not Found!";
}

Post a Comment for "Simple Js Regex To Extract Word Between Parenthesis"