我用一些例子来解释我的问题:
例1:
var str = "this is a test
and also this is a test
and this";
范围:[19 - 22] // "also"
现在,我需要检查str
从19
的起始位置和22
直到该字符串末尾的$
作为此字符true
。如果有,则返回false
,否则返回false
。在这种情况下,输出是:
var str = "this $ is a test
and also this is a test
and this";
例2:
[21 - 24] // "also"
范围:false // there is $ before the range, but there isn't after it, so false
输出:
var str = "this $ is a test
and also this is a test
and $this";
示例3:
[21 - 24] // "also"
范围:true
输出:
var str = "this $ is $ a test
and also this is a test
and $ this";
范例4:
[23 - 26] // "also"
范围:false // there is two $ before the range and that means there isn't $ before the range, so false
输出:
var str = "this $ is $ $ a test
and also this is a test
and $ this";
举例5:
[25 - 27] // "also"
范围:true
输出:
indexOf()
注意:我不需要使用FreeOnTerminate
获取该范围,我已经拥有这些职位。
我该怎么做?
答案 0 :(得分:1)
var n= str.indexOf("also");
var pre=(str.substring(0,n).match(/\$/g) || []).length;
var post=(str.substring(n+3,str.length).match(/\$/g) || []).length;
if(pre %2 ===1 && post %2===1){
console.log(true)
} else {
console.log(false);
}