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

  1. (): matching group
  2. \[: matches [ literal, need escape preceding \
  3. @: matches @ literal
  4. \w+: matches alphanumeric characters , underscore _ 1 or more times
  5. ]: matches ] literal
  6. g: global flag. matches

visualization

enter image description here

demo

var str = '[@name] [@age] years old , favorite color [@color].';    var matches = str.match(/(\[@\w+])/g);    document.write(matches);  console.log(matches);


Comments

Popular posts from this blog

java - UnknownEntityTypeException: Unable to locate persister (Hibernate 5.0) -

python - ValueError: empty vocabulary; perhaps the documents only contain stop words -

ubuntu - collect2: fatal error: ld terminated with signal 9 [Killed] -