How to replace characters in a string with other characters?

前端 未结 1 540
生来不讨喜
生来不讨喜 2021-01-16 18:50

I want to replace certain characters in a string with other characters. I´ve did my research and found that the best way is to use regular expressions... But, something does

相关标签:
1条回答
  • 2021-01-16 19:18

    Match any of those characters using [], and capture the match(es) using () instead of looking for a match of those consecutive characters.

    var url = word.replace(/[áéí]/g, function(s) {
        return alphabet[s];
    });
    

    DEMO: http://jsfiddle.net/5UmLV/1/


    As noted by @Felix Kling the capture group was unnecessary. Updated to reflect that improvement.

    0 讨论(0)
提交回复
热议问题