All I want is this:
The sentences will have some words within % % and I want to extract all of them.
For e.g.
"This is a %varA% and %varB% and that one is a %VarC%"
and when I run it through the Javascript exec I'd like to get %varA%, %varB%, and %varC% in an array.
What would the pattern be? I've tried a number of iterations and none of them gets me each word separately. VarA and VarB etc. will all be words with just a-Z and 0-9, no special characters.
Use String match with /%(\w+)%/g regular expression
Explanation for /%(\w+)%/g
% matches the character % literally(\w+)
\w+ match any word character [a-zA-Z0-9_]
+ Between
one and unlimited times, as many times as possible, giving back as
needed [greedy]% matches the character % literallyg modifier:
global. All matches (don't return on first match)var string = 'this is a %varA% and %varB% and %varC%';
var regExp = /%(\w+)%/g;
document.write(string.match(regExp));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With