...

View Full Version : Regex issue



blinks
06-23-2010, 02:39 AM
I am trying to grab all text between <START> and <END> and have the following bits of code, but neither are returning anything. I use JS so rarely that I can't see where the problems are -



var ermtext = response.match(/<START>(.*?)<END>/i);
if (ermtext) {
result.ermtext = ermtext.replace("$1");
}




var patt=/<START>(.*?)<END>/i;
result.ermtext = response.match(patt, "$1");

randomuser773
06-23-2010, 02:49 AM
var ermtext = response.match(/<START>(.*?)<END>/i);
if (ermtext) {
result.ermtext = ermtext[ 1 ];
}

Keleth
06-23-2010, 02:50 AM
As far as I know, match can give you matches but won't give you back the parenthesis capture, plus match only takes one argument, so the second one is flat out wrong.

Try:

var patt=/<START>(.*?)<END>/i;
document.write(patt.exec(response))

The zeroth element will be the whole match. The first element will be the first parenthesis capture.

EDIT: Oh, I'll just assume randomuser is right, and match does parenthesis capture... so you can go with either method :)

blinks
06-23-2010, 03:46 AM
Thanks randomuser and Keleth; you've helped me get further along, but I seem to have struck another problem. Looks like this time the problem is with the regex, specifically the (.*?) part.

Looks like the dot does not encompass newline character, and I'm guessing that's the issue. But ([.\s]*?) doesn't work?

TIA

blinks
06-23-2010, 04:06 AM
Got it - changed the regex to ((.|\n)*)

Thanks



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum