【问题标题】:Javascript - Search unicode string in unicode stringJavascript - 在 unicode 字符串中搜索 unicode 字符串
【发布时间】:2019-05-12 00:33:15
【问题描述】:

当我尝试在 unicode 字符串中搜索 unicode 字符串时,我找不到解决方案。

例如:检查字符串'vie'是否包含在字符串'Mr. ViỆt has a blue house'

所以我尝试如下:

// Convert string to Unicode
function toUnicode(theString) {
  var unicodeString = '';
  for (var i=0; i < theString.length; i++) {
    var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();
    while (theUnicode.length < 4) {
      theUnicode = '0' + theUnicode;
    }
    theUnicode = '\\u' + theUnicode;
    unicodeString += theUnicode;
  }
  return unicodeString;
}

// Convert string to be Regex Unicode
function toRegexUnicode(theString) {
  var unicodeString = '';
  for (var i=0; i < theString.length; i++) {
    var theUnicode = theString.charCodeAt(i).toString(16).toUpperCase();
    while (theUnicode.length < 4) {
      theUnicode = '0' + theUnicode;
    }
    theUnicode = '\\u' + theUnicode;
    unicodeString += theUnicode;
  }
  return new RegExp('[' + unicodeString + ']')
}

// Search
function searchUnicode() {
    var strOriginal = "Mr. ViỆt has a blue house"
    var regexUnicode = toRegexUnicode(strOriginal)
    var strSearch = toUnicode('vie')
    var result = regexUnicode.test(strSearch)
    console.log(result)
}

测试地址:https://www.w3schools.com/code/tryit.asp?filename=FY3NGXMQRMLA

有没有更好的方法?

【问题讨论】:

    标签: javascript string search unicode


    【解决方案1】:

    首先,您的正则表达式是错误的。去掉大括号。

    其次,您正在以错误的方式创建正则表达式测试。 您当前正在使用完整字符串设置正则表达式搜索。 您也没有将您的 strOriginal 转换为 Unicode。 这意味着您的searchUnicode 函数需要如下所示:

    var strOriginal = "Mr. ViỆt has a blue house"
    var strOriginalUnicode = toUnicode(strOriginal)
    var strSearch = toUnicode('vie')
    var regexUnicode = toRegexUnicode(strSearch)
    var result = regexUnicode.test(strOriginalUnicode)
    

    接下来,我们可以将您的toRegexUnicode 函数简化为:

    // Convert string to be Regex Unicode
    function toRegexUnicode(theString) {
      theString = theString.replace(/\\/g, "\\\\")
      return new RegExp(theString)
    }
    

    无需重复使用您的转换方法。您还将注意到所有\ 的全局替换为\\。这是因为 Regex 将反斜杠视为转义字符,因此我们需要转义转义字符。

    【讨论】:

      【解决方案2】:

      我尝试了另一种方法,将所有字符串转换为 ASCII 然后搜索:

      function stringToASCII(str) {
        try {
          return str.replace(/[àáảãạâầấẩẫậăằắẳẵặ]/g, 'a')
            .replace(/[èéẻẽẹêềếểễệ]/g, 'e')
            .replace(/[đ]/g, 'd')
            .replace(/[ìíỉĩị]/g, 'i')
            .replace(/[òóỏõọôồốổỗộơờớởỡợ]/g, 'o')
            .replace(/[ùúủũụưừứửữự]/g, 'u')
            .replace(/[ỳýỷỹỵ]/g, 'y')
        } catch {
          return ''
        }
      }
      
      function searchASCII() {
        var strOriginal = "Mr. ViỆt lê nguyễn thị tùng á à ạds"
        var strSearch = "vie"
      
        var strOriginalToASCII = stringToASCII(strOriginal.toLowerCase())
        var strSearchToASCII = stringToASCII(strSearch.toLowerCase())
        var result = strOriginalToASCII.includes(strSearchToASCII)
      
        // Results
        console.log('strOriginalToASCII: ', strOriginalToASCII)
        console.log('strSearchToASCII: ', strSearchToASCII)
        console.log('result: ', result)
      }
      

      输出:

      strOriginalToASCII: mr. viet le nguyen thi tung a a ads
      strSearchToASCII: vie
      result: true
      

      测试地址:https://www.w3schools.com/code/tryit.asp?filename=FY3NGXMQRMLA

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-08-27
        • 1970-01-01
        • 1970-01-01
        • 2017-11-19
        • 2012-07-26
        • 1970-01-01
        • 1970-01-01
        • 2011-11-12
        相关资源
        最近更新 更多