www.webdeveloper.com
Results 1 to 6 of 6

Thread: use reg exp to replace strings

  1. #1
    Join Date
    Sep 2011
    Posts
    3

    use reg exp to replace strings

    I want to write a reg exp that replaces strings matching "A anystring" or "An anystring" or "The anystring" with "anystring", basically removing the articles at the beginning of the strings. I got as far as this:

    var pattern:RegExp = /^[a|an|the][\s][\w\s*]/

    but didnt know how to continue. any help please? thanks,

  2. #2
    Join Date
    Nov 2010
    Posts
    1,102
    why not just:

    Code:
    strtomatch="anystring"
    var str="the an but anystring"
    newstr=str.match(strtomatch)
    alert(newstr)
    or simpler, if you don't want to make strtomatch a variable:

    Code:
    var str="the an but anystring"
    newstr=str.match(/anystring/)
    alert(newstr)
    Last edited by xelawho; 09-29-2011 at 06:16 PM.

  3. #3
    Join Date
    Sep 2011
    Posts
    3
    anystring is dynamic, could be anything.

  4. #4
    Join Date
    Nov 2010
    Posts
    1,102
    ahhh, ok got it, sorry.

    so it's just a, an and the? or are there more?

  5. #5
    Join Date
    Oct 2010
    Location
    Versailles, France
    Posts
    1,277
    Try this to remove this three articles and a blanck at the beginning of strings
    HTML Code:
    <script type="text/javascript">
    exp="A anystring|An anystring|The anystring and other words...".split(/\|/g);// to build a table
    for (i=0;i<exp.length;i++) alert(exp[i].replace(/^(An|A|The)\s/,''));
    </script>
    Use square brackets for character class and vertical bar (with parenthesis) to separate alternative patterns. Without parenthesis, you have to write /^An\s|^A\s|^The\s/
    Last edited by 007Julien; 09-29-2011 at 07:15 PM. Reason: complements

  6. #6
    Join Date
    Sep 2011
    Posts
    3
    works like a charm. thank you very much!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles