为了账号安全,请及时绑定邮箱和手机立即绑定

仅当它位于字符串的末尾时才匹配整个句子

仅当它位于字符串的末尾时才匹配整个句子

MMTTMM 2021-12-02 16:51:45
假设我们要检查是否speech包含特定句子 ( reference)但在它的最后和结尾:这是reference:let reference = "have you ever look at someone";基于此,reference我们要比较speech并返回匹配或不匹配:let speech = "blaaaah have you ever look at someone"; // Match because 'have you ever look at someone' is at the end of speechlet speech = "have you ever look at someone blaaaah"; // No match because something else came after 'have you ever look at someone'问题是我无法区分上述语音示例并且代码总是返回匹配://let speech = "blaaaah have you ever look at someone"; // matchedlet speech = "have you ever look at someone blaaaah"; // No match let reference = "have you ever look at someone";if(new RegExp("\\b"+reference+"\\b").test(speech)){  console.log("Finally Matched!")} else {  console.log("No Match At Last!")}
查看完整描述

2 回答

?
守着一只汪

TA贡献1872条经验 获得超3个赞

这是你要找的吗?


//let speech = "blaaaah have you ever look at someone"; // matched

let speech = "have you ever look at someone blaaaah"; // No match 


let reference = "have you ever look at someone";


if(new RegExp("\\b"+reference+"\\b$").test(speech)){

  console.log("Finally Matched!")

} else {

  console.log("No Match At Last!")

}


查看完整回答
反对 回复 2021-12-02
?
拉风的咖菲猫

TA贡献1995条经验 获得超2个赞

您可以使用endsWith如下验证,


var str = "Hello world, welcome to the universe.";

var n = str.endsWith("universe.");

在你的情况下


  //let speech = "blaaaah have you ever look at someone"; // matched

let speech = "have you ever look at someone blaaaah"; // No match 


let reference = "have you ever look at someone";


if(speech.endsWith(reference)){

  console.log("Finally Matched!")

} else {

  console.log("No Match At Last!")

}


查看完整回答
反对 回复 2021-12-02
  • 2 回答
  • 0 关注
  • 178 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信