SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question RegEx Help Needed

    Hi guys

    I need a javascript regex to match the string "<tag>" once only with an index of 0 on a variable called "name".

    If the match is true, to then replace the "<" ">" with an empty string, so we're just left with "tag".

    I'd really appreciate any help.

    Thanks

    Matt

  2. #2
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe this can help you;

    var re = new RegExp("<(tag)>");

    alert("<tag>".replace(re, "$1", ""));
    Teun Hoogendoorn
    ATSC
    LinkedIn
    Blog

  3. #3
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, but it didn't seem to replace "tag".

    Also, I suppose I should have been more specific with "tag". It could be the name of any html tag.

    Thanks in advance.

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, I'm still not sure what you're trying, but the following code maybe helps you out. This code replace a tag (<body> in the example) by antoher tag (<test>).

    var re = new RegExp("<\\w+>");

    alert("<body>".replace(re, "<test>"));

    If you need more help, post an example of the strings you're working with and what should be the outcome of the function.
    Teun Hoogendoorn
    ATSC
    LinkedIn
    Blog

  5. #5
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, that's a bit closer. I'll try to clarify again.

    I need to replace the < and > of a tag with nothing, where I have no idea what the tag name will be.

    So "<body>" becomes "body" and "<h1>" becomes "h1".

    Sorry for the confusion.

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    var re = new RegExp("<(\\w+)>", "g");

    alert("<body>text<h1>etc".replace(re, "$1"));

    This changes: <body>text<h1>etc
    To: bodytexth1etc
    Teun Hoogendoorn
    ATSC
    LinkedIn
    Blog

  7. #7
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's great. Thanks.

    And can I limit it to find only the first tag, starting at the first character? I don't want to replace anything other than that.

    Cheers

  8. #8
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    var re = new RegExp("^<(\\w+)>");

    alert("<body>text<h1>etc".replace(re, "$1"));
    Teun Hoogendoorn
    ATSC
    LinkedIn
    Blog

  9. #9
    SitePoint Member
    Join Date
    Feb 2007
    Posts
    21
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're an absolute legend.

    Thanks for all your help!

  10. #10
    SitePoint Enthusiast
    Join Date
    Sep 2008
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You're welcome
    Teun Hoogendoorn
    ATSC
    LinkedIn
    Blog


Bookmarks

Posting Permissions

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