PDA

View Full Version : I am bad at writing reg. expressions



spetsacdc
Jan 8th, 2009, 01:33 AM
Hey, I am trying to write a regexp that replaces all the div's with the id of h_dash with nothing.


txt.replace("/^<div . id="h_dash" . </div>$/g"," ");

I try to select where something starts with "<div", then has any characters, then contains id="h_dash", then has any characters, and finally ends with a closing </div>.

Any tips?


Thanks

ninnypants
Jan 8th, 2009, 03:51 AM
The sheet at this url should help.
http://www.addedbytes.com/cheat-sheets/regular-expressions-cheat-sheet/

also "." only selects one single character it can be any character but an "\n"(and probably "\r") but it only takes one so if there is another attribute before the id attribute. so you need to use

.*
this will select any number of characters, but will still wont select the "\n" character so you'll want to group some of your characters together with brackets like so

[.\n\r]*
so you expression will be something like this

"/^<div [.\n\r]* id="h_dash" [.\n\r]* </div>$/g"

I'm not sure that this is exactly what you'll need but it should be something similar

Philip M
Jan 8th, 2009, 07:25 AM
so you expression will be something like this

"/^<div [.\n\r]* id="h_dash" [.\n\r]* </div>$/g"

I'm not sure that this is exactly what you'll need but it should be something similar

The spaces within the regex are destructive and will stop it working. A space character must be indicated by \s.

"replace by nothing" is "", not " " which replaces by a space character.

Suggest:-

txt = txt.replace(/^(<div)[.\n\r]*(id="h_dash")[.\n\r]*(</div>)$/gi,"");

jcrypt
Jan 8th, 2009, 09:27 AM
Hey, I am trying to write a regexp that replaces all the div's with the id of h_dash with nothing.


txt.replace("/^<div . id="h_dash" . </div>$/g"," ");

I try to select where something starts with "<div", then has any characters, then contains id="h_dash", then has any characters, and finally ends with a closing </div>.

Any tips?


Thanks
Where are you retrieving the div strings from? If you're trying to use regex on the innerHTML wouldn't it be easier to simply remove the id attributes?


var divs = document.getElementsByTagName('div');
for (var i=0; i<divs.length; ++i) {
if (divs[i].hasAttribute('id) && divs[i].getAttribute('id') == "h_dash") {
divs[i].removeAttribute('id');
}
}

Philip M
Jan 8th, 2009, 12:36 PM
Looking at this again, if the idea is to remove all divs with the id h_dash??? together with their content then this will work:-


<script type = "text/javascript">

var txt = "Something to start with.<div id='h_dash'> Content of the div <br> More Stuff On A Newline<br> Yet More Rubbish On Another Newline</div> Something more not in the div."
txt = txt.replace(/<div.+(id='h_dash).+div>/gi,"");
alert ("txt is now:- " + txt);

</script>

\n and \r are not relevant here. The HTML will contain <br> tags.

If the idea is to simply remove the <div> </div> and id tags leaving the text unaltered, then:-


<script type = "text/javascript">

var txt = "Something to start with.<div id='h_dash34'> Content of the div <br> More Stuff On A Newline <br> Yet More Rubbish On Another Newline</div> Something more not in thie div."
txt = txt.replace(/<div.+(id='h_dash.*'>)/gi,"");
txt = txt.replace(/<\/div>/gi, "")
alert ("txt is now:- " + txt);

</script>