How to extract a string from a larger string?

Using jQuery, how would I find / extract the string "Spartan" if there was the following on the HTML output.

<a href="/mytlnet/logout.php?t=e22df53bf4b5fc0a087ce48897e65ec0">
  <b>Logout</b>
</a> : Spartan<br>
+3
source share
3 answers

Regular expressions . Or splitting a string is more tedious.

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

+3
source

if the pattern is consistent, you can use RegEx

, jQuery HTML Parser

+1

Also, using regular expressions, I also abused these functions to do what you think you need to do (separate html and such from the 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 saved me a lot of time.

0
source

Source: https://habr.com/ru/post/1711044/


All Articles