Got more questions? Find advice on: ASP | SQL | XML | Windows
in Search
Welcome to RegexAdvice Sign in | Join | Help

find word(s) NOT in string of words

Last post 09-07-2008, 4:52 PM by AppDeveloper0918. 12 replies.
Sort Posts: Previous Next
  •  09-03-2008, 8:10 PM 45918

    find word(s) NOT in string of words

    Hi, 

    Using reg exp in javascript, does anybody know how to find a match of WORD(S) (not a character like [^a-zA-Z_]) that are (is) NOT in the reg exp? For example, I have a string "this item is also in my agenda", and I wanna be able to dynamically find all words that are NOT "is", "in" and "also" from the string. How do I do that? 
    Please provide sample code.

    Thanks very much in advance!
  •  09-03-2008, 9:33 PM 45921 in reply to 45918

    Re: find word(s) NOT in string of words

    <script type="text/javascript">
      var re = /(?!(?:i[ns]|also))\b[a-zA-Z_]+\b/g;
      var sourcestring = "this item is also in my agenda";
      var results = new Array();
      var i = 0;
      for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
        results[i] = new Array();
        for (var j=0; j<matches.length; j++) {
          results[i][j]=matches[j];
          alert("results["+i+"]["+j+"] = " + results[i][j]);
        }
        i++;
      }
    </script>

  •  09-03-2008, 10:23 PM 45923 in reply to 45921

    Re: find word(s) NOT in string of words

    Hi ddrukik,

    First of all, thank you very much for the code. It works.

    Question: If I have a value, consisting of these 3 words "in", "is" and "also", which are dynamically coming from the database and currenly populated in a hidden field "MyNoiseWords" as "in|is|also".

    How do I dynamically pass the value of these 3 words into the definition of the reg exp?

    I've tried using the RegExp constructor as: 

           var re = new RegExp("/(?!(?:"+MyNoiseWords.value+"))\b[a-zA-Z_]+\b/g");  // Trying to accomplish this: var re = /(?!(?:in|is|also))\b[a-zA-Z_]+\b/g;

    but it doesn't work and keeps giving me an error.

    How do I fix this?

    Thanks a bunch in advance again!

     

  •  09-03-2008, 11:04 PM 45926 in reply to 45923

    Re: find word(s) NOT in string of words

    Can you tell us what the error you are getting is?

    At a guess, there may be additional characters being added in such as double-quotes around the text. Can you make the substitution into a string which you can then display and then use that string within the regex function?

    Susan

  •  09-03-2008, 11:23 PM 45928 in reply to 45923

    Re: find word(s) NOT in string of words

    <html>
    <body>
    <input type="hidden" id="secretID" value="in|is|also">
    <script type="text/javascript">
      var pattern='(?!(?:'+document.getElementById("secretID").value+'))\\b[a-z_]+\\b';
      var re = new RegExp(pattern,'gi');
      var sourcestring = "this item is also in my agenda";
      var results = new Array();
      var results='';
      for (var matches = re.exec(sourcestring); matches != null; matches = re.exec(sourcestring)) {
        for (var j=0; j<matches.length; j++) {
          results=results+"\nMatch: '"+matches[j]+"'";
        }
      }
      alert(results);
    </script>
    </body>
    </html>
  •  09-04-2008, 2:51 AM 45931 in reply to 45926

    Re: find word(s) NOT in string of words

    Thank you, Aussie Susan. 

    I think ddrudik just gave me the code similar to what you suggested. I think that'll work. I will give it a try tomorrow when I get into my office.

    Thanks a bunch to both of you again.

  •  09-04-2008, 2:51 AM 45932 in reply to 45928

    Re: find word(s) NOT in string of words

    Thank you very much, ddrudik.
  •  09-04-2008, 1:40 PM 45968 in reply to 45928

    Re: find word(s) NOT in string of words

    It works perfectly. Thank you very much, ddrudik.

  •  09-04-2008, 2:16 PM 45975 in reply to 45968

    Re: find word(s) NOT in string of words

    Glad to help.

    Note that since this pattern doesn't use any capture groups ( ) in it, you can safely replace:

        for (var j=0; j<matches.length; j++) {
          results=results+"\nMatch: '"+matches[j]+"'";
        }

    With:

          results=results+"\nMatch: '"+matches[0]+"'";


  •  09-04-2008, 2:30 PM 45978 in reply to 45975

    Re: find word(s) NOT in string of words

    Great. I'll replace that.

    Thank you very much!

  •  09-07-2008, 4:50 AM 46083 in reply to 45921

    Re: find word(s) NOT in string of words

    Hi ddrudik,

    Would you please analyze this line for me:  /(?!(?:i[ns]|also))\b[a-zA-Z_]+\b/g

    Thanks very much in advance!

     

  •  09-07-2008, 10:05 AM 46085 in reply to 46083

    Re: find word(s) NOT in string of words

    ----------------------------------------------------------------------
      (?!                      look ahead to see if there is not:
    ----------------------------------------------------------------------
        (?:                      group, but do not capture:
    ----------------------------------------------------------------------
          i                        'i'
    ----------------------------------------------------------------------
          [ns]                     any character of: 'n', 's'
    ----------------------------------------------------------------------
         |                        OR
    ----------------------------------------------------------------------
          also                     'also'
    ----------------------------------------------------------------------
        )                        end of grouping
    ----------------------------------------------------------------------
      )                        end of look-ahead
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------
      [a-zA-Z_]+               any character of: 'a' to 'z', 'A' to 'Z',
                               '_' (1 or more times (matching the most
                               amount possible))
    ----------------------------------------------------------------------
      \b                       the boundary between a word char (\w) and
                               something that is not a word char
    ----------------------------------------------------------------------


  •  09-07-2008, 4:52 PM 46089 in reply to 46085

    Re: find word(s) NOT in string of words

    Thank you very much again, ddrudik.

View as RSS news feed in XML