By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,722 Members | 1,252 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,722 IT Pros & Developers. It's quick & easy.

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

P: n/a
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[i]+"\"<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+myA rray[0].length)+"#<br>");
document.write("<hr>");

count++;
}
</script>
</body>
</html>
--
my IE6 favorites (bookmarks URL's)
http://www.myjavaware.co.uk/bookmark.htm
Jul 20 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"Neil Morris" <Ne********@btinternet.com> writes:
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
--
Lasse Reichstein Nielsen - lr*@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
'Faith without judgement merely degrades the spirit divine.'
Jul 20 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.