Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Regular Expression Help

    OK I am wanting a RegularExpression which will match a few different words (ie. a|b|c|d) and then replace the matched word with itself enclosed in <b></b> or some other such HTML tag.

    Currently I am having to run a different RegExp for each word

    ex.
    var GT2 = GT.replace(/ O:/g, " <br><b>O:</b>");
    var GT3 = GT2.replace(/ SO:/g," <br><b>SO:</b>");
    var GT4 = GT3.replace(/ R:/g," <br><b>R:</b>");
    var GT5 = GT4.replace(/ WL:/g," <br><b>WL</b>");

    What I want is something like

    var GT2 = GT.replace (/ O:| SO:| R:| WL:/g, "<br><b>word replaced here</b>")

    Is there any way to do this?

    Thanks for any help

    Darksbane

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Posts
    3,467
    Thanks
    0
    Thanked 0 Times in 0 Posts
    yup -- they're called backreferences, or, remembered matches.

    GT = GT.replace(/(O:|SO:|R:|WL:)/g, " <br><b>$1</b>");

    With some exceptions, any parenthical in a pattern creates a remembered match, which you reference by it's number, up to 9. As you can see, the syntax for placing a remembered match into the replacement is "dollar-sign reference number"

    Check into a good tutorial on regular expressions, they will cover this topic better.
    My Site | fValidate | My Brainbench | MSDN | Gecko | xBrowser DOM | PHP | Ars | PVP
    “Minds are like parachutes. They don't work unless they are open”
    “Maturity is simply knowing when to not be immature”

  • #3
    New Coder
    Join Date
    Jun 2002
    Posts
    42
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool thanks beetle, I knew I had done this before in other languages but couldn't remember what it was called!

    Thanks

  • #4
    Master Coder
    Join Date
    Feb 2003
    Location
    Umeå, Sweden
    Posts
    5,575
    Thanks
    0
    Thanked 83 Times in 74 Posts
    liorean <[lio@wg]>
    Articles: RegEx evolt wsabstract , Named Arguments
    Useful Threads: JavaScript Docs & Refs, FAQ - HTML & CSS Docs, FAQ - XML Doc & Refs
    Moz: JavaScript DOM Interfaces MSDN: JScript DHTML KDE: KJS KHTML Opera: Standards


  •  

    Posting Permissions

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