javascript - 我对 string.match(part) (javascript) 有疑问

标签 javascript regex match

var string = $.trim("How are you ? are you fine ?");
var part = $.trim("How are you ? are you fine ?");

var SearchResult = string.match(part);

if (SearchResult != null && part!="") {
    alert("hello1");
}


string = $.trim("How are you ? a");
part = $.trim("How are you ? a");

SearchResult = string.match(part);

if (SearchResult != null && part!="") {
    alert("hello2");
}

string = $.trim("How are you ?");
part = $.trim("How are you ?");

SearchResult = string.match(part);

if (SearchResult != null && part!="") {
    alert("hello3");
}

只有第三个警报有效;第一个和第二个字符串有什么问题?

最佳答案

我认为问题是字符串中有一个问号,它被解析为特殊的正则表达式字符。您应该尝试将其转义为 \?

关于javascript - 我对 string.match(part) (javascript) 有疑问,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3635257/

相关文章:

c# - 用唯一值替换字符串中单词的每个实例

python - 查找最佳子字符串匹配

javascript - 覆盖 jquery 菜单的外部 CSS 规则

javascript - 在javascript中合并两个对象

javascript - 使用撇号 html 代码传递 js 参数

c# - 分割并添加空间

javascript - 根据长度(<4)提取特殊字符之间的子串,并将缺少的字符替换为 0,使其变为 4

javascript - 从特定点捕获字符串

rust - 如何在匹配中使用表达式作为条件?

javascript - 如何使用Jquery编写firefox点击事件代码