1554 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

I think these needs more than one regex.

Try Google to find a pattern.

and don’t forget the ‘g’ modifier to match them all

Can you give me 2 regex example for any of my numbers.

i am using this script:
{
    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();

in variable str have string

var str= new RegExp('(89789)-897','gi');

i want this

var str= new RegExp(SomeVariable,'gi');

and SomeVariable also checked replace alter from 2 or more regex.

Example flow: start SomeVariable = (123) 456.7894

middle SomeVariable = regex get string and replace alter.

End
var str= new RegExp(SomeVariable,'gi');

This what i want, Its possible :)

2024 posts
  • Elite Author: Sold more than $75,000 on Envato Market
  • Has sold $250,000+ on Envato Market
  • Sells items exclusively on Envato Market
  • Contributed a free file of the month
+8 more
revaxarts says

Start here:

http://jsfiddle.net/JYYQ8/

as I sad, you have to search for the correct pattern

1554 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

Start here: http://jsfiddle.net/JYYQ8/ as I sad, you have to search for the correct pattern
Revaxart WaAOn your script runs nicely :) but why it replace in this format:
(800).123.4456 to GHOST.GHOST.GHOST
i want to this replacement:
(800).123.4456 to GHOST
and if want to add
 <span>tofindstring</span>
means add
<span></span> tag before and after how to do this.
http://jsfiddle.net/JYYQ8/
15 posts
  • Has been part of the Envato Community for over 4 years
  • Has collected 10+ items on Envato Market
  • Sells items exclusively on Envato Market
ChristianB says

What are you trying to achieve?

Are you calling the function for a specific element? or the whole page? Then, are you matching it to so if it’s a valid North America phone number? Then, if it does match, you want to ‘encase’ it in span elements and change the text?

1554 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

What are you trying to achieve? Are you calling the function for a specific element? or the whole page? Then, are you matching it to so if it’s a valid North America phone number? Then, if it does match, you want to ‘encase’ it in span elements and change the text?

yes you right, i find a string “0800.123.1267” its example, and add span tag before and after. means

0800.123.1267 to <span>0800.123.1267</span>
.

1554 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

I think you’re spending too much time on a phone number!

hehahahah yes.. a one water drop when continues droping on stone, next you know wht i am saying in this line.

1554 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 after so many hickupps i figured out with all(TF Author) of your helps. my code now working what i need but the problem is if string found too many time in page it replace the first string found on page i want to replace all of match string here is my:
<p>(800)123 1234</p>
<p>(800)123 1234</p>
<p>(800)123 1234</p>
<p>(800)123 1234</p>
<p>(800)123 1234</p>

  <script type="text/javascript">
var avidno = '(800)123 1234';
var bodytext = document.body.innerHTML;
function validate () {
    var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/gi;

   if (regex.test(avidno)) {
        alert('bingo');
        var altrstr = '<span>'+avidno+'</span>';
        var newaltr = bodytext.replace(avidno, altrstr);
        document.body.innerHTML = newaltr
        alert(newaltr);
        // Valid international phone number
    } else {
        alert('uupss');
        // Invalid international phone number
    }
}
validate();
</script>

1554 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

Friends i got quite some success but at the replace through an undefined error:

here is my new code:

var avidno = '(800)123 1234';
var bodytext = document.body.innerHTML;
var newaltr
function validate () {
    var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/gi;

   if (regex.test(avidno)) {
        alert('bingo');
        var altrstr = '<span>'+avidno+'</span>';
        //var newaltr = bodytext.replace(avidno, altrstr);
        //var str_count = bodytext.match(avidno).length;  //4 matched

        document.body.innerHTML = newaltr;
        alert(avidno.length);
        find_count = avidno.length;
        for(var i=0;i<find_count;i++)
        {
            //alert("bing");
          newaltr = bodytext.replace(avidno, altrstr);
          document.body.innerHTML = newaltr
        }

        // Valid international phone number
    } else {
        alert('uupss');
        // Invalid international phone number
    }
}
validate();
1554 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

Friends i got success regarding for string replace task with all of your’s helps, specially thanks to revaxarts, ChristianB, SportTipsWorld and amatyr4n.

see how i have done:
var avidno = '800.123.1234';
var bodytext = document.body.innerHTML;
var newaltr;
function validate () {
    var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

   if (regex.test(avidno)) {
        alert('bingo');
        var altrstr = '<span>'+avidno+'</span>';
        var newaltr = bodytext.replace(new RegExp(avidno, "g"), altrstr);
        document.body.innerHTML = newaltr;
        // Valid international phone number
    } else {
        alert('uupss');
        // Invalid international phone number
    }
}
validate();

Thankx to my all friends :)

by
by
by
by
by
by