如何检查字符串是否包含不同语言的特定单词

时间:2018-04-13 08:02:02

标签: javascript

我有一个写函数,用于计算字符串中的输出,语言为英语,函数为true,但是有多种其他语言,如阿拉伯语,函数无效



    function countWordInString(str, word){
     var str = str.toUpperCase();
     var count =  str.match( new RegExp('\\b' + word.toUpperCase() +'\\b','g') ); 
     return count? count.length : 0;
    }

// Language English: 

        var result = countWordInString('hello world','hello');
        console.log(result);// 1 => true

// Language Arabic: 

        var result = countWordInString('باركود','باركود');
        console.log(result);// 0=>false




0 个答案:

没有答案