开发者

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).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜