regular expression to negate the start of line char '^'?

N

Neil Morris

Hi
I have the following code that lists first names of people who's
surnames are 'Morris'. What I am trying to do is search for first names that
don't start at the beginning of the line ie. have the '[^\^]' in the regExp
object, but 'Neil' is matched even though it is next to(?) the start of line
char(?) or should I say 'Neil' is the first 'word' of the string?

here's the code

<html>
<head>
<title>
my RegExp
</title>
</head>
<body>
<script>
var myPat = new RegExp("([^\\^]\\w+) Morris","gi");
var str = new String("Neil Morris Lee Morris Jenny Morris Jackie Morris mai
Morris wong Morris write Morris");
var count=1;
while(myArray=myPat.exec(str)) {
for(i=1;i<myArray.length;i++) {
document.write(+count+"=array\["+i+"\] \""+myArray+"\"<br>");
}
document.write("RegExp objects methods and results<br>");
document.write("myPat.lastIndex="+myPat.lastIndex+"<br>");
document.write("myPat.source="+myPat.source+"<br>");
document.write("array from RegExp object exec method<br>");
document.write("method index="+myArray.index+"<br>");
document.write("method input="+myArray.input+"<br>");
document.write("method \[0\]=#"+myArray[0]+"<br>");
document.write("string before
match=#"+myArray.input.substring(0,myArray.index)+"#<br>");
document.write("string after
match=#"+myArray.input.substring(myArray.index+myArray[0].length)+"#<br>");
document.write("<hr>");

count++;
}
</script>
</body>
</html>
 
L

Lasse Reichstein Nielsen

Neil Morris said:
I have the following code that lists first names of people who's
surnames are 'Morris'. What I am trying to do is search for first names that
don't start at the beginning of the line ie. have the '[^\^]' in the regExp
object,

That regular expression matches the character "^", and only that.
but 'Neil' is matched even though it is next to(?) the start of line
char(?) or should I say 'Neil' is the first 'word' of the string?

If you want to check that something is not at the beginning of the line,
just check that something occurs before it. Perhaps even check that
something non-whitespace occurs before it:

/\S\s+Morris/

(something non-whitespace followed by one or more whitespace followed by
"Morris")
/L
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top