1551 posts
  • Has been part of the Envato Community for over 3 years
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
webdesignerart says

Hi friends,

i want to replace this”(800) 123 456” to “some string”. i put this code:
$('body').html($('body').html().replace('(800) 123 456','GHOST'));

it can’t replace.

but when i put this
$('body').html($('body').html().replace('(800) ','GHOST'));

It works nice.

why it not replace out of the round bracket string.

plz i need some help.

309 posts
  • Has been part of the Envato Community for over 3 years
  • Has referred 10+ members
  • Located in Austria
  • Has sold $10,000+ on Envato Market
+2 more
squaredWeb says

No clue at all,

but i cant wait to see what you are developing with all those number replacements via regex, js and god knows what ^^

1551 posts
  • Has been part of the Envato Community for over 3 years
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
webdesignerart says

hehahahha ok see what i am actually doing: i want to replace (89789)-897 to my any string :)


(89789)-897<br />
(89789)-897<br />
(89789)-897<br />
(89789)-897<br />
(89789)-897<br />
(89789)-897<br />
(89789)-897<br />
(89789)-897<br />

<script type="text/javascript"> 
function checkPhone() 
{
    var str= new RegExp('(89789)-897','gi');
    var phone2 = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/gi;

         var chk_str_body = $('body').html(); 
         var patt1=str
         var str_count = chk_str_body.match(patt1).length;  //4 matched

         alert('count:' +str_count);

    if (str.test(phone2)) {

        alert('true');
     } else {

        for(var i=0;i<str_count;i++)
        {
          $('body').html($('body').html().replace(str,'GHOST'));
        }

     }
}

checkPhone();
</script>

in this code string found but replace not works.

BUT

when i change the top line code to this:
var str= new RegExp('(89789)','gi');

it works fine.

basically it regex work with (value)

and not work with (value)value

Plz help me to solve this issue.

293 posts
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Contributed a free file of the month
+4 more
amatyr4n says

Your first example works just well: http://jsfiddle.net/jTMJz/

1551 posts
  • Has been part of the Envato Community for over 3 years
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
webdesignerart says

Your first example works just well: http://jsfiddle.net/jTMJz/

Sir your replacing string with this ’’ element, the problem is replacing string in this pattern

 $('body').html($('body').html().replace(/(800) 123 456/g,'GHOST'));
293 posts
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Contributed a free file of the month
+4 more
amatyr4n says


Your first example works just well: http://jsfiddle.net/jTMJz/

Sir your replacing string with this ’’ element, the problem is replacing string in this pattern

 $('body').html($('body').html().replace(/(800) 123 456/g,'GHOST'));

Ok now I see what you mean. Some special chars treated differently in RegExp not like usual String, in this case you have to escape the brackets, see http://jsfiddle.net/cx2zj/ ;)

1234 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
MBMedia says

Regular expressions in any language aren’t just a literal strings to be matched, they are basically a language with their own syntax, and parentheses are part of the regular expression syntax.

So the regex /(800) 123 456/g doesn’t see those parentheses as parentheses in the string, it sees that as part of the regex syntax of you using parentheses to group some keys together, just with only 1 key inside so basically the parentheses just get ignored. So it sees /800 123 456/g as the pattern to match, which isn’t a pattern that matches any part of your string!

If you want parentheses to be interpreted as part of the characters in the match you need to escape them: /\(800\) 123 456/g. I tested it out and it worked fine for me in chrome this way.

1234 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
MBMedia says

Some special chars treated differently in RegExp not like usual String, in this case you have to escape the brackets, see http://jsfiddle.net/cx2zj/ ;)

beat me to it :)

1234 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 50+ members
  • Has sold $75,000+ on Envato Market and is now an Elite Author
  • Has collected 50+ items on Envato Market
+5 more
MBMedia says

Some special chars treated differently in RegExp not like usual String, in this case you have to escape the brackets, see http://jsfiddle.net/cx2zj/ ;)

beat me to it :)

1551 posts
  • Has been part of the Envato Community for over 3 years
  • Has been a beta tester for an Envato feature
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
webdesignerart says



Your first example works just well: http://jsfiddle.net/jTMJz/

Sir your replacing string with this ’’ element, the problem is replacing string in this pattern

 $('body').html($('body').html().replace(/(800) 123 456/g,'GHOST'));
Ok now I see what you mean. Some special chars treated differently in RegExp not like usual String, in this case you have to escape the brackets, see http://jsfiddle.net/cx2zj/ ;)
friend your very close :) thnkx but the problem is”
$('body').html($('body').html().replace(str,'GHOST'));
i have var “str” in replace find area to work with this situtation:
$('body').html($('body').html().replace(/\(800\)\s123\s456/g,'GHOST'));
by
by
by
by
by
by