...

View Full Version : regex



BubikolRamios
12-19-2010, 11:16 PM
Should alert '' twice , instead alerts original text


text = 'p18=100';
text = text.replace("(^|&)p100=18.*?(&|$)","")
alert(text);
text = text.replace(/(^|&)p100=18.*?(&|$)/g,"")
alert(text);


tested regex on online tester and it should be OK.

?

jmrker
12-20-2010, 12:53 AM
Should alert '' twice , instead alerts original text


text = 'p18=100';
text = text.replace("(^|&)p100=18.*?(&|$)","")
alert(text);
text = text.replace(/(^|&)p100=18.*?(&|$)/g,"")
alert(text);


tested regex on online tester and it should be OK.

?

Your match to replace with does not equal the original. :eek:


<script type="text/javascript">

var text = 'p18=100';
var otxt = text;
var txt = /^p18=100\.*/;
text = text.replace(txt,"")
alert('Old: '+otxt+'\nNew: '+text);

</script>

Philip M
12-20-2010, 10:10 AM
<script type="text/javascript">
text = 'p100=18';
text = text.replace(/(^|&)p100=18.*?(&|$)/ ,"")
alert (text);
text = text.replace(/(^|&)p100=18.*?(&|$)/g,"")
alert (text);
</script>

Note that ^ and $ are literal characters in your regex - not start and end of string. So if you really want these they must be escaped.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

jmrker
12-20-2010, 04:50 PM
...

Note that ^ and $ are literal characters in your regex - not start and end of string. So if you really want these they must be escaped.

Some people, when confronted with a problem, think "I know, I'll use regular expressions." Now they have two problems." — Jamie Zawinski.

Nice catch ... I was in a PERL mindset. :o

BubikolRamios
12-20-2010, 05:55 PM
[CODE]
Note that ^ and $ are literal characters in your regex - not start and end of string. So if you really want these they must be escaped.


Yeah, I realy want those.
It took mi some time to see why it does not match.
if I would not been told to, probably wouldnt spot it at all. :)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum