How to extract a string from a larger string?

萝らか妹 提交于 2019-12-02 00:22:37

问题


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

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

回答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).




回答2:


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

or if the markup is going to be the same you can try the 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):

//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.



来源:https://stackoverflow.com/questions/1030127/how-to-extract-a-string-from-a-larger-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!