Javascript Regex Replace string, special character need help

Hi friends,

I am using javasrcript for string replacement,

i m currently doing replace.(string,tothisstring)

i have a regex for valid north American phone number here it is:

var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

its check valid phone number like (800)123 1234, 800.123.1456 etc etc formats.

i want to replace (800)123 1234 to (800)123 1234

i dont know $1 to $2 replacement.

friends did you have any solution for it.

webdesignerart said

Hi friends,

I am using javasrcript for string replacement,

i m currently doing replace.(string,tothisstring)

i have a regex for valid north American phone number here it is:

var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

its check valid phone number like (800)123 1234, 800.123.1456 etc etc formats.

i want to replace (800)123 1234 to (800)123 1234

i dont know $1 to $2 replacement.

friends did you have any solution for it.

Arent:

i want to replace (800)123 1234 to (800)123 1234

The same ??

i wanted replace or add HTML element span tag to (800)123 4587

means (800)123 4587 to (800)123 4587

here is function i am using:

var avidno = '1234567890';
function validate () {
	var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
	
   if (regex.test(avidno)) {
		alert('bingo');
		var altrstr = avidno.replace(regex, ' $2');
		alert(altrstr);
        // Valid international phone number
    } else {
		alert('uupss');
        // Invalid international phone number
    }
}

validate();

i want regex that replace or add span before and after
12345678 to 123546789

webdesignerart said

i want regex that replace or add span before and after
12345678 to 123546789

The two numbers are the same, in case you are using some tags, the forum doesn’t display them. It’s not easy to guess what you possibly mean :slight_smile:

Always easier to create a fiddle

http://jsfiddle.net/

he typed

(800)123 4587 to (800)123 4587

Ok then use this tool.

http://regex.larsolavtorvik.com/

firennnndddddddsssssss its my mistake if forget that when posting html elements in forum add

 tag before and after 

friends here is my exact code:


var avidno = '1234567890';
function validate () {
    var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

   if (regex.test(avidno)) {
        alert('bingo');
        var altrstr = avidno.replace(regex, ' $2');
        alert(altrstr);
        // Valid international phone number
    } else {
        alert('uupss');
        // Invalid international phone number
    }
}

validate();

i want to replace number(800)123 45678 to

(800)123 4587

Basically i want to add

mynumber
before and after to the number :slight_smile:

ANy one here! to help me.

SportTipsWorld said
webdesignerart said

Hi friends,

I am using javasrcript for string replacement,

i m currently doing replace.(string,tothisstring)

i have a regex for valid north American phone number here it is:

var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;

its check valid phone number like (800)123 1234, 800.123.1456 etc etc formats.

i want to replace (800)123 1234 to (800)123 1234

i dont know $1 to $2 replacement.

friends did you have any solution for it.

Arent:

i want to replace (800)123 1234 to (800)123 1234

The same ??

Friend ok its possibe from regex add

 tage before and after the number 

You’re going all the wrong way it seems, surely you’re just doing string concatenation?

e.g.

var withSpans = "" + altrstr + "";
ChristianB said

You’re going all the wrong way it seems, surely you’re just doing string concatenation?

e.g.

var withSpans = "" + altrstr + "";

yes thnkx for your time, basically i get value from input field "800.123.4567
and replace it with

value before and after on runtime i am replacing
number on page. hope you understand.

I just hope that any validation you’re doing for storage, is not done soley client-side, as it’s easily comprimised.

use backslash ‘’ to escape characters such as ‘.’, ‘$’, etc. (ones you want to be a part of the search string). for example: [-.]

designcise said

use backslash ‘’ to escape characters such as ‘.’, ‘$’, etc. (ones you want to be a part of the search string). for example: [-.]

Friend i m replacing string with new string if like this (800)xxxx xxxx means string brackets
i can’t or can’t replace runtime on page this is the isssuee.

the brackets need to be escaped too doing something like this: (800) otherwise it considers it a “group” in regexp.

designcise said

the brackets need to be escaped too doing something like this: (800) otherwise it considers it a “group” in regexp.

OOOoooHH You Right now you understand what i going do doing. the problem is

i am replace

replace(varofstring, ’ varofstring’);

in varofstring some time number is 800.132.1245 or (800)123.1234 or different different north American number format. but when i replacing bracket numbers my code not works :frowning:
webdesignerart said

here is function i am using:

var avidno = '1234567890';
function validate () {
	var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
	
   if (regex.test(avidno)) {
		alert('bingo');
		var altrstr = avidno.replace(regex, ' $2');
		alert(altrstr);
        // Valid international phone number
    } else {
		alert('uupss');
        // Invalid international phone number
    }
}

validate();

i want regex that replace or add span before and after
12345678 to 123546789

Why you need to use replace() if you just want to add < span > tag around it? try this instead

var avidno = '(800)123 1234';
function validate () {
	var regex = /^\(?([0-9]{3})\)?[-. ]?([0-9]{3})[-. ]?([0-9]{4})$/;
	
   if (regex.test(avidno)) {
		alert('bingo');
		var altrstr = ''+avidno+'';
		alert(altrstr);
        // Valid international phone number
    } else {
		alert('uupss');
        // Invalid international phone number
    }
}

validate();

http://digitalbush.com/projects/masked-input-plugin/#demo