javascript - RegEx to match words in comma delimited list -
how can match text appears between delimiters, not match delimiters themselves?
text
donotfindme('donotfindme') donotfindme(findme) donotfindme(findme,findme) donotfindme(findme,findme,findme)
script
text = text.replace(/[\(,]([a-za-z]*)[,\)]/g, function(item) { return "'" + item + "'"; });
expected result
donotfindme('donotfindme') donotfindme('findme') donotfindme('findme','findme') donotfindme('findme','findme','findme')
here's pretty simple way it:
([a-za-z]+)(?=,|\))
this looks word succeeded either comma or close-parenthesis.
var s = "donotfindme('donotfindme')\ndonotfindme(findme)\ndonotfindme(findme,findme)\ndonotfindme(findme,findme,findme)"; var r = s.replace(/([a-za-z]+)(?=,|\))/g, "'$1'" ); alert(r);
used same test code other 2 answers; thanks!
Comments
Post a Comment