Find uppercase substrings and wrap with acronym tags
For example replace the string Yangomo, Congo, DRC
with Yang开发者_运维知识库omo, Congo, <acronym>DRC</acronym>
. There may potentially be mulitple uppercase substings in each string. I assume some form of regex?
Thanks.
Well, a really simple one might be:
var replaced = original.replace(/\b([A-Z]+)\b/g, '<acronym>$1</acronym>');
Doing this sort of thing always has complications, however; it depends on the source material. (The "\b" thing matches word boundaries, and is an invaluable trick for all sorts of occasions.)
edit — insightful user Buh Buh points out that it might be nice to only affect strings with more than two characters, which would look like /\b([A-Z]{2,})\b/
.
Personally I would use PHP to explode the string, use a regex to find all uppercase letters /[A-Z]+/
and then use PHP to insert the tags (using str_replace).
精彩评论