-
October 17th, 2014, 05:22 PM
#1
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
-
October 19th, 2014, 03:00 PM
#2
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.
-
October 20th, 2014, 06:58 AM
#3
Re: How to replace a specific string present in HTML tags?
Originally Posted by PeejAvery
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>this rpg</et>">this isn't it</span>
how about
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.
-
October 21st, 2014, 04:01 PM
#4
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|