Asim Zaidi
Asim Zaidi

Reputation: 28284

parsing out in javascript

I want to check a string and if it has no <br /> starting then I don't wanna do any thing
for example mysqtinr = 23435-acs
as you can see no <br /> starting but if the string has the following then I wanna parse it out

myString = <br /> some text goes here <b> Some other things </b>: just more test <b> http://website/index.php </b> on line <b> 43 </b> <br /> 1234-acd

I want to parse out everything after the last <br />

how can I do that thanks

Upvotes: 1

Views: 80

Answers (2)

Tim Pietzcker
Tim Pietzcker

Reputation: 336128

var myregexp = /<br\s*\/>((?:(?!<br\s*\/>).)*)$/;
var match = myregexp.exec(subject);
if (match != null) {
    result = match[1];
} else {
    result = "";
}

Explanation:

<br\s*/>        # match <br /> with optional space
(               # capture the following:
 (?:            # repeat the following, but don't capture (because the outer parens do so already)
  (?!<br\s*/>)  # assert that it's impossible to match <br />
  .             # if so, match any character
 )*             # do this as many times as necessary...
)               # (end of capturing group)
$               # ...until the end of the string.

So we first try to match <br/> or <br /> etc. If that fails, the match fails. If not, then we capture every following character until the end of the string unless it's possible to match another <br/> along the way. This ensures that we are indeed matching from the last <br/> onwards. The result will be in backreference nr. 1 (which may be empty if there is nothing after the last <br/>).

Upvotes: 1

Alex Reitbort
Alex Reitbort

Reputation: 13696

var index = myString.lastIndexOf("<br />");
if(index > 0)
    myString = myString.substring(index + "<br />".length);

Upvotes: 2

Related Questions