Javascript正则表达式,用于匹配包含特殊字符的整个单词

时间:2016-12-17 02:50:12

标签: javascript regex

我正在尝试使用javascript正则表达式匹配整个确切的单词。

鉴于字符串:1)"我知道C ++。" 2)"我认识Java。"

我尝试使用new Regex('\\b' + text + '\\b', 'gi'),这对于没有像示例#2这样的特殊字符的单词非常有效。

我也看过这个网址: Regular expression for matching exact word affect the special character matching

并实施了:

escaped = escaped.replace(/^(\w)/, "\\b$1"); escaped = escaped.replace(/(\w)$/, "$1\\b");

,这将匹配text = 'C++'(它将匹配两个示例)

然而,如果有人输入拼写错误,字符串是"我也知道C ++。",当我不想要它时,后者的正则表达式仍将与C ++匹配,因为单词" C ++也是#34;与text = 'C++'不完全匹配。

除非C ++既是单词的前面,也是单词的结尾,否则我可以进行哪些更改以使其不匹配。

3 个答案:

答案 0 :(得分:1)

如果whole words including special characters表示所有 [\r\n\t\f\v ],您只需执行以下操作:

const REGEX = /([^\s]+)+/g;

function selectWords(string) {
  const REGEX = /([^\s]+)+/g;
  
  return string
  
    // remove punctuation
    .replace(/[^a-z0-9\s+#]/ig, "")
  
    // perform the match
    .match(REGEX)

    // prevent null returns
    || []
  ;
}

var text = "Hello World"
var [first, second, ...rest] = selectWords(text);
console.log(1, {first, second, rest});


// example with punctuation
var text = "I can come today, she said, but not tomorrow."
var [first, second, third, ...rest] = selectWords(text);
console.log(2, {first, second, third, rest});


// example with possible throw
var text = ",.'\"`  \r"
var [first, second, third, ...rest] = selectWords(text);
console.log(3, {first, second, third, rest});

// example with a specific word to be matched
function selectSpecificWord(string, ...words) {
  return selectWords(string)
    .filter(word => ~words.indexOf(word))
  ;
}
var expected = "C++";
var test = "I know C++";
var test1 = "I know C++AndJava";

console.log("Test Case 1", selectSpecificWord(test, expected));
console.log("Test Case 2", selectSpecificWord(test1, expected));

答案 1 :(得分:1)

您可以在单词字符后添加一系列可接受的字符([+#]):

str = 'I know C++too. I know Java and C#.';
console.log(str.match(/(\w[+#]+|\w+)/g));

注意:\w[+#]+必须放在交替表达式的第一位,以优先于更通用的\w+

答案 2 :(得分:0)

使用此((?:(?:\w)+?)(?=\b|\w[-+]{2,2})(?:[-+]{2,2})?)

我还为一个例子添加了-符号。见in life

enter image description here