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 strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Replace spaces for "and" 1

Status
Not open for further replies.

vasilek

Programmer
Jan 5, 2003
99
US
Hello. I would like to replace all spaces in a string for " and ". For example: if user enters "Canon digital camera" in text field I would like to convert it to "Cannon and digital and camera",

Thanks a bunch!
 
Like this:
mystring.replace(/ /g," and ");

but I hope you're not using JavaScript to write SQL. That's just too easy to hack.

Adam

Pedro offers you his protection
 

Why would you want to convert "Canon" to "Cannon"?

Dan


The answers you get are only as good as the information you give!

 
BillyRayPreachersSon, I hoped it would make some booms :)

adam0101, this works, thanks. But now I realized: what if the user will mistakenly type "Canon digital camera", with two spaces. The result will be Canon and and digital and camera.

How this can be corrected?

Thanks.
 
Then first convert all the multiple spaces (including possible triples or more) to single spaces:

Code:
while (mystring.indexOf('  ') > -1)
  {
  mystring.replace(/  /g," ");
  }
mystring.replace(/ /g," and ");

Lee
 
Or this.
[tt] mystring.replace(/ +/g," and ");[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top