Case insensitive regex in JavaScript

前端 未结 4 1069
-上瘾入骨i
-上瘾入骨i 2020-11-22 12:33

I want to extract a query string from my URL using JavaScript, and I want to do a case insensitive comparison for the query string name. Here is what I am doing:

<         


        
4条回答
  •  长情又很酷
    2020-11-22 13:06

    Simple one liner. In the example below it replaces every vowel with an X.

    function replaceWithRegex(str, regex, replaceWith) {
      return str.replace(regex, replaceWith);
    }
    
    replaceWithRegex('HEllo there', /[aeiou]/gi, 'X'); //"HXllX thXrX"
    

提交回复
热议问题