最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Modify RegEx Match in JavaScript - Stack Overflow

programmeradmin4浏览0评论

I want to wrap all occurrences of certain words in a given string with square brackets in JavaScript.

Say these words are apples, oranges, and bananas. Then a subject text "You are paring apples to oranges." should turn into "You are paring [apples] to [oranges]."

The regular expression for this would be (apples|oranges), but the question is how to wrap or more generally, modify each match. String.replace() lets you replace matches founds with some pre-defined value, rather a value based on the match.

Thanks.

I want to wrap all occurrences of certain words in a given string with square brackets in JavaScript.

Say these words are apples, oranges, and bananas. Then a subject text "You are paring apples to oranges." should turn into "You are paring [apples] to [oranges]."

The regular expression for this would be (apples|oranges), but the question is how to wrap or more generally, modify each match. String.replace() lets you replace matches founds with some pre-defined value, rather a value based on the match.

Thanks.

Share Improve this question edited Jan 26, 2011 at 20:21 Tom Tucker asked Jan 26, 2011 at 19:54 Tom TuckerTom Tucker 11.9k23 gold badges93 silver badges130 bronze badges 1
  • 2 Actually, the regexp for that would be (apples|oranges)... – gnarf Commented Jan 26, 2011 at 20:04
Add a ment  | 

4 Answers 4

Reset to default 7
js> var str = 'You are paring apples to oranges.';
js> str.replace(/(apples|oranges)/g, '[$1]')
You are paring [apples] to [oranges].

If you prefer a function where you can simply feed an array of words:

function reg_quote(str, delimiter) {
    return (str+'').replace(new RegExp('[.\\\\+*?\\[\\^\\]$(){}=!<>|:\\'+(delimiter || '')+'-]', 'g'), '\\$&');
}

function mark_words(str, words) {
    for(var i = 0; i < words.length; i++) {
        words[i] = reg_quote(words[i]);
    }
    return str.replace(new RegExp('(' + words.join('|') + ')', 'g'), '[$1]')
}

Demo:

js> mark_words(str, ['apples', 'oranges']);
You are paring [apples] to [oranges].
js> mark_words(str, ['apples', 'You', 'oranges']);
[You] are paring [apples] to [oranges].

If you want it case insensitive, replace 'g' with 'gi'.

In addition to the simple substitution string mentioned by others, you can also pass a function to String.replace() which is called for each match, and whose return value is substituted into the resulting string. This lets you do more plicated transformations. For details, see:

https://developer.mozilla/en/JavaScript/Reference/Global_Objects/String/replace#Specifying_a_function_as_a_parameter

'You are paring apples to oranges.'.replace(/(apples|oranges)/g, "[$1]");   
//"You are paring [apples] to [oranges]."

This is a very ugly code, but it does the job:

    var string = "You are paring apples to oranges";
    var regEx = "(apples|oranges)";
    var re = new RegExp(regEx, "g");
    for (var i=0; i<string.match(re).length; i++)
    {
        string = string.replace(string.match(re)[i], "[" + string.match(re)[i] + "]");
    }
    alert(string);
发布评论

评论列表(0)

  1. 暂无评论