什么是正则表达式(在JavaScript中,如果重要的话)只匹配文本是否完全匹配?也就是说,字符串的另一端不应该有额外的字符.
例如,如果我想匹配abc,然后1abc1,1abc和abc1将不匹配.
如果给你一个字符串 like"foo bar 32536364"和一个 regex /foo bar \d+/,你如何检查正则表达式是否匹配完整字符串而不是字符串中的某个子字符串?此外,我无法更改输入值,例如 put^和$.
谢谢
我想检查字符串是否(let entry)包含与以下内容完全匹配let expect:
let expect = 'i was sent'
let entry = 'i was sente to earth' // should return false
// let entry = 'to earth i was sent' should return true
// includes() using the first instance of the entry returns true
if(entry.includes(expect)){
console.log('exact match')
} else {
console.log('no matches')
}Run Code Online (Sandbox Code Playgroud)
StackOverflow 上有很多答案,但我找不到可行的解决方案。
笔记:
let expect = 'i was sent'
let entry = 'to earth i was sent'
Run Code Online (Sandbox Code Playgroud)
应该返回 true
let expect = 'i …Run Code Online (Sandbox Code Playgroud) 我有正则表达式接受1到100之间的数字.但它接受特殊字符'%'
这是我正在使用的正则表达式
^(0*100{1,1}\\.?((?<=\\.)0*)?%?$)|(^0*\\d{0,2}\\.?((?<=\\.)\\d*)?%?)$'
Run Code Online (Sandbox Code Playgroud)
我不想接受%任何人都可以帮我解决这个问题.