PDA

View Full Version : Help with parsetext and value.replace



SuperLes2006
01-18-2006, 09:36 AM
I'm trying to remove a the characters "//" from a line of parsed text.
The text being parsed looks like this......

//PackerSetting=0//0.0 cm
SpringSetting=10//200 N/mm
Gear1Setting=0//13/41 (12.615)

I would like to remove only the double instance of the characters "//"
and not the single instance of "/" so the text would look like this

PackerSetting=00.0 cm
SpringSetting=10200 N/mm
Gear1Setting=013/41 (12.615)

Here's the code I am using..


function parsetext( value )
{
value == ( "" );
return value.replace(/["//"]/g, ""); value;
}


The Problem is that it removes the single instance of "/" also and I end up with this..

PackerSetting=00.0 cm
SpringSetting=10200 Nmm
Gear1Setting=01341 (12.615)

How do I remove the double instance only? :confused:

jscheuer1
01-18-2006, 10:47 AM
Your function is a bit more complicated than need be. Also, your regular expression is selecting anything that matches any single character within the brackets [] (that is the meaning of brackets in this context). So, " will match as will /. Since it has the g flag, it selects all instances. Your regex could be simply:


/\/\//g

The \ character is the escape character telling the regex that the two //'s are not the end of the regex. I've simplified your function and made a small demo:


<div id="txt"><pre>
//PackerSetting=0//0.0 cm
SpringSetting=10//200 N/mm
Gear1Setting=0//13/41 (12.615)
</pre>
</div>
<script type="text/javascript">

String.prototype.parsetext=function (){
return this.replace(/\/\//g, "");
}

document.getElementById('txt').innerHTML=document.getElementById('txt').innerHTML.parsetext()
</script>

Or, even more simply:


<script type="text/javascript">
document.getElementById('txt').innerHTML=document.getElementById('txt').innerHTML.replace(/\/\//g, "");
</script>

SuperLes2006
01-19-2006, 04:36 AM
Brilliant!!!

I got it worked out now.
Thanks a bunch :)

I'm getting some excellent help in this forum!