Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/90.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正则表达式_Javascript_Html_Css - Fatal编程技术网

用于在两个字符串之间查找子字符串的Javascript正则表达式

用于在两个字符串之间查找子字符串的Javascript正则表达式,javascript,html,css,Javascript,Html,Css,我试图创建一个JS函数来查找以下文本的子字符串(REGEX)。我正试图在一个你愿意提问的问题中改变选项的颜色 字符串示例 Would you rather be on a survival reality show or dating game show? Would you rather <span class="highlight1"> be on a survival reality show </span> or <span class="highligh

我试图创建一个JS函数来查找以下文本的子字符串(REGEX)。我正试图在一个你愿意提问的问题中改变选项的颜色

字符串示例

Would you rather be on a survival reality show or dating game show?
Would you rather <span class="highlight1"> be on a survival reality show </span> or <span class="highlight2"> dating game show? </span>
解决方案示例

Would you rather be on a survival reality show or dating game show?
Would you rather <span class="highlight1"> be on a survival reality show </span> or <span class="highlight2"> dating game show? </span>
JS:

$(文档).ready(函数(){
$('.question').html(函数(q,html){
返回html.replace(**我在这里放什么**,$1');
});});
正则表达式-在上述代码中不起作用

(?<=Would you rather)(.*)(?=or) 1st Part

(?<=or)(.*)(?=) 2nd Part

(?仅获取问题第一部分的正则表达式可以是

/^Would you rather(.*)(:? or .*)$/
                       ^^ find 'or' and the rest of question

我不知道,你想用它做什么,而不是
alert
可以是任何
$(this.html(…)
或其他东西

改变两部分,如

/^(Would you rather)(.*)(:? or )([^?]+)\?$/

仅获取问题第一部分的正则表达式可以是

/^Would you rather(.*)(:? or .*)$/
                       ^^ find 'or' and the rest of question

我不知道,你想用它做什么,而不是
alert
可以是任何
$(this.html(…)
或其他东西

改变两部分,如

/^(Would you rather)(.*)(:? or )([^?]+)\?$/
let res=html.replace(/(?
let res=html.replace(/(?

var html='你更愿意参加生存真人秀还是约会游戏秀?'
html.replace(/^(您愿意\s)(.*)(\sor\s)(.*)\?$/g,`1$2$3$4?`)

var html='你更愿意参加生存真人秀还是约会游戏秀?'
html.replace(/^(您愿意\s)(.*)(\sor\s)(.*)\?$/g,`1$2$3$4?`)

JS regex语法是
html.replace(/your regex here/,“$1”);
我不希望结果是什么,所以只需要这个基本建议。我只是不明白/your regex here/部分……我如何转换(?结果应该是什么样子的?您在正则表达式中查找了两个部分,但只在span中取了一个部分…请添加任何示例或字符串和结果。让我们看看第一部分。(?JS正则表达式语法为
html.replace(/your regex here/,“$1”)
我不想要结果,所以我只需要这个基本的建议。这里是/your regex/部分,我不明白…我如何转换(?结果应该是什么样子?你在正则表达式中看两个部分,但在跨度内只取一个部分…请添加任何示例或字符串和结果。让我们看看第一部分。(?+1…我在你的问题中没有看到未格式化的
解决方案示例
,因此我尝试了第一个代码…你需要第二个,对吗?很高兴帮助你!是的,第二个为我做了!我不知道你们是如何编写正则表达式的。我每次都被it@DuvonLawrence:历史上多次尝试,在数千个正则表达式之后,这是一个惯例-)+1…我在你的问题中没有看到未格式化的
解决方案示例
,因此我尝试了第一个代码…你需要第二个,对吗?很高兴帮助你!是的,第二个为我做了!我不知道你们是如何编写正则表达式的。我每次都被it@DuvonLawrence:历史上多次尝试,在数千个正则表达式之后,这是一个惯例。:-)请提供有关您正在使用的方法的更多信息,以便人们将来能从您的答案中受益。@Notflip如何?您可以将其添加到您的答案中。请提供有关您正在使用的方法的更多信息,以便人们将来能从您的答案中受益。@Notflip如何?您可以将其添加到您的答案中。

var html = 'Would you rather be on a survival reality show or dating game show?'
html.replace(/^(Would you rather\s)(.*)(\sor\s)(.*)\?$/g,`$1<span class="red">$2</span>$3<span class="red">$4</span>?`)