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

Discussion in 'Javascript' started by Neil Morris, Jul 15, 2003.

  1. Neil Morris

    Neil Morris Guest

    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>


    --
    my IE6 favorites (bookmarks URL's)
    http://www.myjavaware.co.uk/bookmark.htm
    Neil Morris, Jul 15, 2003
    #1
    1. Advertising

  2. "Neil Morris" <> 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 -
    Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit.html>
    'Faith without judgement merely degrades the spirit divine.'
    Lasse Reichstein Nielsen, Jul 15, 2003
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Summercool
    Replies:
    13
    Views:
    651
    Dr.Ruud
    Feb 1, 2008
  2. Replies:
    11
    Views:
    261
    Raul Parolari
    Dec 2, 2007
  3. Summercool
    Replies:
    22
    Views:
    398
    Ryan Holmes
    Aug 6, 2010
  4. Sherm Pendley

    need to negate regex in middle of expression

    Sherm Pendley, Jun 20, 2005, in forum: Perl Misc
    Replies:
    8
    Views:
    148
    Tad McClellan
    Jun 20, 2005
  5. Summercool
    Replies:
    14
    Views:
    183
    Dr.Ruud
    Feb 1, 2008
Loading...

Share This Page