Jump to content
Sign in to follow this  
rongyne

HTML DOM with Javascript

Recommended Posts

Here is my problem: I want to to make a script that will search for somthing in the format of:(bluetext) This text is blue (/bluetext)remove the entire thing, remember the position, remove the beginning and end and re-insert the text as blue.Of course this is just an example.Can anyone help me?

Share this post


Link to post
Share on other sites

could always do a while loop on something like.match(/\((\w+)text\)(.*)\(\/(\w+)text\)/i) != nulland you'll have RegExp.$1 and RegExp.$3 as "blue" and RegExp.$2 as This text is blueProof of concept:

<script type="text/javascript">var x = "(bluetext)this text is blue(/bluetext)";if(x.match(/\((\w+)text\)(.*)\(\/(\w+)text\)/i) != null){ alert(RegExp.$1); alert(RegExp.$2); alert(RegExp.$3);}</script>

Try it in: http://www.w3schools.com/js/tryit.asp?file...yjs_bodysection

Share this post


Link to post
Share on other sites

???????????????????I am not quite sure what that means, but I will try it.Um, to be quite frank, I do not know what this means:/\((\w+)text\)(.*)\(\/(\w+)text\)/iBasically what I am trying to do is create a way for others to modify what they write, sort of like UBBC.

Share this post


Link to post
Share on other sites

Wouldn't you prefer to do this server side?The rather jumbled up set I put up bascially means:/............... /iThis is the RegExp itself, with case set to insensitive. This means (bLuEteXt) and (BLUETEXT) and (bluetext) will be found.\( and \)Where you come across these, it's basically the start and end of (bluetext), but you have to slash them so javascript knows to take them as literal characters. You'll see why in the next example.(\w+)textthis means, find any word that has text attached to the end of it. so redtext sometext pinkytext whitetext chequetext... anything as long as its letters.(.*)Anything at all. Could be "My 1 $$$ string". As long as it isn't a new line, it'll accept it.So, just put it altogether, and you've got:open reg exp, backslash the ( then any word with text on the end, then the closing ) then anything at all in between, then an open (, then a backslashed /, then another any word with text on the end, then a closing ) then close the reg exp and add the case insensitiveHope you can understand that. It's pretty hard to explain RegExp if you haven't got a good grasp of it, so might be worth reading over a few tutorials around if you still can't quite understand my poor explanations :)

Share this post


Link to post
Share on other sites

that's really confusing.. perhaps creating a set of replace statements would be easier.. for example:

<script type="text/javascript">var x = "(bluetext)This is blue text!(/bluetext)";var x = x.replace('(bluetext)','<font color="blue">');var x = x.replace('(/bluetext)','</font>');document.write(x);</script>

very simple right now.. but with a bit of work you could make it work..try it: http://www.w3schools.com/js/tryit.asp?filename=tryjs_text

Share this post


Link to post
Share on other sites

You'd want to make sure there's the same amount of (bluetext) as (/bluetext) on the page then though, or you could end up with a lot of open HTML tags in your document.

Share this post


Link to post
Share on other sites
:):):(:D:) Thanks both!I think I need study up on regular expressions. I do conceptually understand it. :blink: Um, still confused.How would I go about replacing text in the html document? Example<HTML><HEAD></HEAD><BODY>(bluetext)anything(/blutext)<script>deletes (bluetext)anything(/bluetext) after storing it in var x;does replacementswrites it back in.</script></body</HTML>Basically, my original idea was to store the entire html, do the replacements, and open a docuement with document.open(), and write it in. So I could do it more easily if I could store the HTML.I couldn't figure out how to do this, so I got to asking how to do a find, replace.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×