str = "find().nodes() as n3" ==> find str = "with n1,n2,n3" ===> with
Use string match with the regex pattern ^\w+:
match
^\w+
var inputs = ["find().nodes() as n3", "with n1,n2,n3"]; inputs.forEach(x => console.log(x + " => " + x.match(/^\w+/)));