Reputation: 1333
I want to extract a string using RegEx in Javascript for example
StudentName nameX = John;
or
StudentName nameX;
I want to extract only "nameX" this is what i've tried so far.
var name = allName.match("StudentName(.*);|StudentName(.*)=");
what I'm getting is : "nameX = John" , but I'm not getting "nameX" only.
Upvotes: 1
Views: 141
Reputation: 74018
Try this non greedy pattern
var name = allName.match("StudentName\\s*(.*?)\\s*[=;]");
Upvotes: 2
Reputation: 43663
Use regex pattern inside of match
match(/StudentName\s+(\w+)/)[1]
See this demo.
Upvotes: 2
Reputation: 5822
If you split on blank spaces then the second match at index 1 should contain the name.
var name = allName.split(/[ ;]/g)[1];
Upvotes: 1