1

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

1
  • 1
    I've passed out using JavaScript once. :P Commented Sep 14, 2010 at 23:29

2 Answers 2

2
var index = myString.lastIndexOf("<br />");
if(index > 0)
    myString = myString.substring(index + "<br />".length);
Sign up to request clarification or add additional context in comments.

6 Comments

This doesn't work as the tag <br/> and <br /> is valid. See jsfiddle.net/y38QH
What do you mean by valid? he wants the string after last br tag
I mean that if you try to parse a string that has <br/> your method won't find it.
The question was about <br /> specifically. But the example can easily be expanded to support other tags/tag representaions like <br>, <br/> or < br >.
The example also didn't have single or double quotes around it. My point being, parsing HTML directly with javascript functions substr and indexOf is only setting him up for future headaches.
|
1
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/>).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.