PDA

View Full Version : Please Help Me Understand What Is Wrong Here - REGEX:Javascipt



thePirate
17 Nov 2010, 06:23 AM
Hi all,

I am a newbie who is learning Javascript after PHP. I have been with PHP for over 6 Months and today have myself in an awkward situation with Regex of Javascript.

I am trying to parse an input on a TextArea. The Input has -
[Name - Xyz]
[Standard - II]

1. 30 2. 40 3. 50

The code should extract and break this input into two Text Areas -
1. Containing the Name and the Standard with the Square Brackets
2. The Marks in the Subjects

Here is my code that is giving me all the problems -
function doParseInp()
{
//remove Leading Spaces - Trim
var lines;
var TA=document.getElementById('inp').value;
document.getElementById('inp').value = TA.replace(/^\s+|\s+$/g, '');
var TA=document.getElementById('inp').value;
if(document.all) { // IE
lines=TA.split("\r\n");
}
else { //Mozilla
lines=TA.split("\n");
}
for(var i=0; i<lines.length; i++) {
//if the lines start with [ they must be put in the stInfo Text Box and removed from Here
if(!lines[i].search(/^\[/))
{
document.getElementById("stInfo").value += lines[i];
}

}
}

Unfortunately as you may see above if(!lines[i].search(/^\[/)) gets me the lines that start with [ and this is what my confusion is. From my knowledge PHP says ^ symbol is to mean the start of the string(here Line) but if I do Not use the ! I get wrong results....

Please correct me as this would be something that I would cherish all long my career.

Regards,
thePirate