How to ignore acute accent in a javascript regex match?

前端 未结 4 2058
南笙
南笙 2020-11-30 14:44

I need to match a word like \'César\' for a regex like this /^cesar/i.

Is there an option like /i to configure the regex so it ignores the

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-30 15:31

    In general it could by done easily with some help of Lodash library, when you're testing two types of the same regex input - one is deburred by lodash _deburr function other is raw user's input. Example for username input form based filtering (with little help of lodash _filter function):

    import _ from 'lodash'
    
    const input = inputFromYourFormInput
    const users = ['Jiřina Žlutá', 'Aleš Úzký'];
    const regex = new RegExp(input, 'gi');
    
    const filtered = _.filter(users, (u) => {
      const debured = _.deburr(u)
      if (u.name.match(regex) || debured.match(regex))
        return u
    })
    

    In this case It doesn't matter if your input is Jirina Zluta or Jiřina Žlutá _filter function will return new filtered array [Jiřina Žlutá]

提交回复
热议问题