如何从较大的字符串中提取字符串?

时间:2022-05-23 19:19:02

Using jQuery how would I find/extract the "Spartan" string if the outputted HTML page had the following..

使用jQuery如果输出的HTML页面具有以下内容,我将如何找到/提取“Spartan”字符串。

<a href="/mytlnet/logout.php?t=e22df53bf4b5fc0a087ce48897e65ec0">
  <b>Logout</b>
</a> : Spartan<br>

3 个解决方案

#1


Regular Expressions. Or by splitting the string in a more tedious fashion.

常用表达。或者以更繁琐的方式拆分字符串。

Since I'm not a big regex-junkie, I would likely get the text-equivalent using .text(), then split the result on ":", and grab the second index (which would be the 'Spartan' text).

由于我不是一个大的正则表达式瘾君子,我可能会使用.text()获得文本等效,然后将结果拆分为“:”,并获取第二个索引(这将是'Spartan'文本)。

#2


if the pattern is going to be consistent you can your RegEx

如果模式一致,您可以使用RegEx

or if the markup is going to be the same you can try the jQuery HTML Parser

或者如果标记将是相同的,您可以尝试jQuery HTML Parser

#3


As well as using regular expressions, I've also abusively used these functions to do the things it seems you want to do (strip html and such from a text string):

除了使用正则表达式之外,我还滥用这些函数来执行您想要执行的操作(从文本字符串中删除html等):

//removes all HTML tags
function striptags(stringToStrip) {
    return stringToStrip.replace(/(<([^>]+)>)/ig,"");
}
//standard trim function for JavaScript--removes leading and trailing white space
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}

Incredible regular expressions that have saved me a lot of time.

令人难以置信的正则表达式为我节省了大量时间。

#1


Regular Expressions. Or by splitting the string in a more tedious fashion.

常用表达。或者以更繁琐的方式拆分字符串。

Since I'm not a big regex-junkie, I would likely get the text-equivalent using .text(), then split the result on ":", and grab the second index (which would be the 'Spartan' text).

由于我不是一个大的正则表达式瘾君子,我可能会使用.text()获得文本等效,然后将结果拆分为“:”,并获取第二个索引(这将是'Spartan'文本)。

#2


if the pattern is going to be consistent you can your RegEx

如果模式一致,您可以使用RegEx

or if the markup is going to be the same you can try the jQuery HTML Parser

或者如果标记将是相同的,您可以尝试jQuery HTML Parser

#3


As well as using regular expressions, I've also abusively used these functions to do the things it seems you want to do (strip html and such from a text string):

除了使用正则表达式之外,我还滥用这些函数来执行您想要执行的操作(从文本字符串中删除html等):

//removes all HTML tags
function striptags(stringToStrip) {
    return stringToStrip.replace(/(<([^>]+)>)/ig,"");
}
//standard trim function for JavaScript--removes leading and trailing white space
function trim(stringToTrim) {
return stringToTrim.replace(/^\s+|\s+$/g,"");
}

Incredible regular expressions that have saved me a lot of time.

令人难以置信的正则表达式为我节省了大量时间。