如何在javascript中将正则表达式与Google搜索更正字符串进行匹配

时间:2023-01-14 14:05:31

I need to use regex to get the "Did you mean? portion of the source code from a google search. I am not aware of any difference between the regex in C# and in javascript, but

我需要使用正则表达式从谷歌搜索获得“你的意思吗?”部分源代码。我不知道C#和javascript中的正则表达式之间有什么区别,但是

This is the regex I have in C#:

这是我在C#中使用的正则表达式:

output = output.Replace("\"", "");
string regex = "Did you mean: </span><a href=/search.[a-zA-Z0-9=&;_-]{1,}q=[a-zA-Z0-9+-]{1,}";

This is what i have in javascript:

这就是我在javascript中所拥有的:

var response = this.responseText.replace("\"", "")
var regex = new RegExp("Did you mean: </span><a href=.search.[a-zA-Z0-9=&;_-]{1,}q=[a-zA-Z0-9+-]{1,}")

This is part of the response I am getting back from google:

这是我从谷歌回复的回复的一部分:

style="color:#cc0000">Did you mean: </span><a href=/search?hl=en&amp;safe=off&amp;&amp;sa=X&amp;ei=DXtLTd2hKYjKgQfJ0sBD&amp;ved=0CBIQBSgA&amp;q=Linkin+Park-In+The+End&amp;spell=1"class=spell>Linkin Park-In <b><i>The</i></b> End</a>&nbsp;&nbsp;<br></div><!--a--><h2 class=hd>Search Results</h2><div id=ires><ol><li class="g videobox" id=videobox><h3 class=r><a href="/search?q=Linkin+Park-In+Th+End&amp;hl=en&amp;

How can I make the javascript regex match correctly?

如何正确地使javascript正则表达式匹配?

NOTE: I already know that the C# regex only matches once on the page.

注意:我已经知道C#正则表达式只在页面上匹配一次。

1 个解决方案

#1


3  

Why use regex here?

为什么在这里使用正则表达式?

If you notice the source code, the Did you Mean section is inside a Div which has ID topstuff. So you can get the innerHtml of this Div.

如果您注意到源代码,则“您的意思是”部分位于具有ID topstuff的Div内。所以你可以得到这个Div的innerHtml。

#1


3  

Why use regex here?

为什么在这里使用正则表达式?

If you notice the source code, the Did you Mean section is inside a Div which has ID topstuff. So you can get the innerHtml of this Div.

如果您注意到源代码,则“您的意思是”部分位于具有ID topstuff的Div内。所以你可以得到这个Div的innerHtml。