CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Sep 2014
    Posts
    6

    How to replace a specific string present in HTML tags?

    How do I replace the string "rpg" only present in between <et> tags ?

    HTML Code:
    <et>ReplaceOnlyrpg</et>

    After replacement it should look like this
    HTML Code:
    <et>ReplaceOnly</et>
    Thank you

  2. #2
    Join Date
    May 2002
    Posts
    10,943

    Re: How to replace a specific string present in HTML tags?

    A simple regular expression will do it. In what language are you wanting to do it?
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

  3. #3
    Join Date
    Apr 2000
    Location
    Belgium (Europe)
    Posts
    4,626

    Re: How to replace a specific string present in HTML tags?

    Quote Originally Posted by PeejAvery View Post
    A simple regular expression will do it. In what language are you wanting to do it?
    ONLY with a lot of conditions and assumptions. If you need this to work on "any" kind of valid (and potentially invalid) html, it could be difficult to impossible to do with a regex.

    HTML is not a regular language, so RegEx cannot possibly parse every possible usage case.

    Do you want it (or not) replaced in:
    <span xdata="<et>thisrpg</et>">this isn't it</span>
    how about
    <et>
    this rpg
    </et>
    how about repetitions ?
    <et>this rpg and that rpg and another rpg and rpgrpgrpg</et>
    what if there's extra tag ?
    <et>this in another tag <span>rpg</span></et>
    or what about attributes of et
    <et attr="rpg">none here</et>

    If you need it to work on every possible kind of html, you'll need a proper html parser.
    <et> isn't a valid HTML tag btw, did you mean XML instead ? if so, you need a xml parser. The advantage here is that they're easier to come by than HTML parsers.

    Since this is about javascript, are you talking about parsing your own DOM ? or about some HTML coming from elsewhere, you can use the loaded DOM at least to fetch the <et> tags, then just walk over the collection if that's what you're after.
    Last edited by OReubens; October 20th, 2014 at 07:02 AM.

  4. #4
    Join Date
    May 2002
    Posts
    10,943

    Re: How to replace a specific string present in HTML tags?

    Yes, of course almost any regular expression has exceptions. But the example given was pretty basic. Since the OP said between tags, I'm guessing he didn't expect to have them inside the tags in the form of attributes or attribute values.

    PHP Code:
    <script type="text/javascript">
      var 
    str "<et>rpgReplacerpgOnlyrpg</et>";
      var 
    re = new RegExp("(rpg)""gm");
      
    new_str str.replace(re"");
      
    document.write(new_str);
    </
    script
    If the post was helpful...Rate it! Remember to use [code] or [php] tags.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured