Replacing multiple patterns in a block of data

后端 未结 4 676
遇见更好的自我
遇见更好的自我 2020-12-31 14:48

I need to find the most efficient way of matching multiple regular expressions on a single block of text. To give an example of what I need, consider a block of text:

<
4条回答
  •  囚心锁ツ
    2020-12-31 15:28

    You can pass a function to replace:

    var hello = "Hello World what a beautiful day";
    hello.replace(/Hello|World/g, function ($0, $1, $2) // $3, $4... $n for captures
    {
        if ($0 == "Hello")
            return "Bye";
        else if ($0 == "World")
            return "Universe";
    });
    
    // Output: "Bye Universe what a beautiful day";
    

提交回复
热议问题