Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

str.replace 2

Status
Not open for further replies.

Smoogan

Programmer
Feb 21, 2008
21
0
0
GB
I have the following javascript:

str = item.value;
var pattern1 = /, /g;
var pattern2 = /,/g;
str = str.replace(pattern1, ",");
str = str.replace(pattern2, ", ");
item.value = str;

The problem is... each replace works, but not together.

The code works on a textarea and automatically ads a space after a comma, but catches if the users presses space after they type comma.

So it would replace:

", " to ", "
and
"," to ", "

Any help??

Thanks!!
Adam
 
Fixed it...

str = item.value;
var pattern1 = /, /g;
var pattern2 = /, /g;
var pattern3 = /,/g;
str = str.replace(pattern1, ",");
str = str.replace(pattern2, ",");
str = str.replace(pattern3, ", ");
item.value = str;


Ok... now i change the script so it will look less prettier.

item.value = item.value.replace(/, /g, ",").replace(/, /g, ",").replace(/,/g, ", ");

And now it works...
 
item.value = item.value.replace(/,(\s{1,2})?/g, ",")
 
That doesn't have the effect I want, would need to be:

item.value = item.value.replace(/,(\s{1,2})?/g, ",").replace(/,/g, ", ");
 
yeah, I missed the space, but it's still prettier.
 
I need to do the same thing but with forward slashes, having a problem with it?

Should this work:
item.value = item.value.replace(/,(/{1,2})?/g, ",").replace(/,/g, "/ ");

??
 
Ok... i should of look over that before I posted that...

Now working...
item.value = item.value.replace(/[/](\s{1,2})?/g, "/").replace(/[/]/g, "/ ");
 
not sure about the brackets, but, .replace(/\/(\s{1,2})?/g, "/ "); should work.
 
Well the brackets seem to work... not really too hot on regular expressions.
 
me neither really, but Kaht and Tsuji aren't around :)
 
me neither really, but Kaht and Tsuji aren't around :)

Thanks, I will take that as a compliment [smile]

Smoogan, since you're a new user to the site I'll point out that it's nice to click the "Thank xxxxx for this valuable post!" link when someone provides you helpful information. Since MarkZK took the time to help you out with your problem, you should throw him a star - it gives him an vote for tipmaster of the week and helps get his name up on the MVP board for this forum.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Thanks kaht,

Have done that now, thanks MarkZK.

Also...

Can you explain how to user back/forward slashes correctly in regexp?

Thanks
 
Smoogan, after you click that link you a popup box will appear and you have to confirm your decision. Since there is no purple star appearing next to MarkZK's name, that means that the vote was not completed.

With regards to your question, any time that you use a forward slash in a regexp, it should be escaped by a preceding backslash. The shortcut version for a regexp in javascript is by encapsulating it in 2 slashes. So this regexp would match any consective "abc" characters in a string:
Code:
var regex = [!]/[/!]abc[!]/[/!];

Now, imagine that you wanted to search for just a forward slash using a regexp. If you write it like this:
Code:
var regex = [!]//[/!]/;
The javascript compiler is going to think that you intended to close the regexp on the second forward slash - since a forward slash signifies the start and end of the regexp. Additionally, you're going to get an error on this line because the compiler will now see the rogue / hanging off the end of the declaration - likely causing a syntax error. So, to signify that we are actually looking for a forward slash in your regexp, instead of trying to terminate the regexp, we need to add a backward slash directly before the forward slash to signify that we're "escaping" the next character. The javascript compiler then interprets this character as the literal forward slash character, and then the 3rd / signifies the end of the regexp:
Code:
var regex = [!]/[/!]\/[!]/[/!];

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Ah.. Done ;)

Just becuase I wondered... what if you was seaching for instances of \/?

How do you write this?
 
When you're searching for the backslash character, it has to be escaped the same as the forward slash character. Remember that putting the backslash character only escapes [!]the next character[/!].

So, you would write it like this:
Code:
var regex = [!]/[/!][b][blue]\[/blue][/b]\[b][blue]\[/blue][/b]/[!]/[/!]

The red slashes are the start and end of the regexp. The first blue backslash escapes the following character - a backslash, and the second blue backslash escapes the following character - a forward slash.

-kaht

Lisa, if you don't like your job you don't strike. You just go in every day and do it really half-assed. That's the American way. - Homer Simpson

[small]<P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <B> <P> <.</B>[/small]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top