How do I find "mergefields" using javascript and regex? -
my "mergefields" [@something].
having string this..
[@name] [@age] years old , favorite color [@color].
since user enters mergefields, don't know mergefields.
how can in javascript + regex find [@...] fields?
thanks
you can use following regex extract [@...]
pattern string.
/(\[@\w+])/g
explanation
()
: matching group\[
: matches[
literal, need escape preceding\
@
: matches@
literal\w+
: matches alphanumeric characters , underscore_
1 or more times]
: matches]
literalg
: global flag. matches
visualization
var str = '[@name] [@age] years old , favorite color [@color].'; var matches = str.match(/(\[@\w+])/g); document.write(matches); console.log(matches);
Comments
Post a Comment