让我们需要定位的字符为$,我们将验证它是否位于'和'之间。代码看起来像这样:
var str = " Verify '$' "
var str2 = " Let's see if ' The $ is inside ! ' "
var str3 = " I got $ "
console.log(verify(str)) //=> true
console.log(verify(str2)) //=> true
console.log(verify(str3)) //=> false
Javascript String.prototype.includes(),可用于验证字符串中是否存在$,但我不理解如何验证它是否在'和 '?
您可以寻找$
,由'
包裹。
const check = string => /'.*\$.*'/.test(string);
console.log([
" Verify '$' ",
" Let's see if ' The $ is inside ! ' ",
" I got $ "].map(check)
);
这可以使用正则表达式来完成:
function verify(input)
{
const regex = RegExp(/'.*?\$.*?'/);
return regex.test(input);
}
var str = " Verify '$' "
var str2 = " Let's see if ' The $ is inside ! ' "
var str3 = " I got $ "
console.log(verify(str)) //=> true
console.log(verify(str2)) //=> true
console.log(verify(str3)) //=> false