Skip to content
Advertisement

How to rematch the first word or ‘(‘ the first word at the beginning

str = “find().nodes() as n3” ==> find
str = “with n1,n2,n3” ===> with

Advertisement

Answer

Use string match with the regex pattern ^w+:

var inputs = ["find().nodes() as n3", "with n1,n2,n3"];
inputs.forEach(x => console.log(x + " => " + x.match(/^w+/)));
Advertisement