Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/416.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS将RegEx与While循环相结合_Javascript_Regex_While Loop - Fatal编程技术网

Javascript JS将RegEx与While循环相结合

Javascript JS将RegEx与While循环相结合,javascript,regex,while-loop,Javascript,Regex,While Loop,尝试搜索特定文本并检查行中是否存在该文本。如果显示“是”,如果显示“否”,则显示“否” 目前我拥有: const str = `Hello my name is Stack Overlow.` const publish = /Hello/g; let pub; while ((pub = publish.exec(str)) !== null) { if (pub.index === publish.lastIndex) { publis

尝试搜索特定文本并检查行中是否存在该文本。如果显示“是”,如果显示“否”,则显示“否”

目前我拥有:

const str = `Hello my name is Stack Overlow.`
const publish = /Hello/g;
    let pub;
    while ((pub = publish.exec(str)) !== null) {
        if (pub.index === publish.lastIndex) {
            publish.lastIndex++;
        }
        pub.forEach((ko, groupIndex) => {
            document.write(`"Exists": "${ko}",<br>`);
        });
    }
const str=`你好,我的名字是Stack Overlow`
const publish=/Hello/g;
让我们去酒吧;
while((pub=publish.exec(str))!==null){
if(pub.index==publish.lastIndex){
publish.lastIndex++;
}
pub.forEach((ko,groupIndex)=>{
write(`Exists:“${ko}”,
`); }); }
试试这个:

var string='Hello我的名字不重要';
var模式=/Hello/g;
if(模式测试(字符串)){
console.log('yes');
}
否则{
console.log('no');

}
如果您对正则表达式不太感兴趣,也可以使用
indexOf()
执行此操作:

const str='Hello我的名字是Stack Overlow'
const containsSubstring=(字符串,子字符串)=>~string.indexOf(子字符串)?'是:'否';

log(containsSubstring(str,'Hello'));/'是的
毫无疑问。另外,代码中的“是”和“否”在哪里?