4 Replies - 1614 Views - Last Post: 11 December 2012 - 05:47 PM

#1 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Strip single html spaces in javascript

Posted 11 December 2012 - 04:56 PM

Here is what I want to try to accomplish in javascript:

My String: Adam  is here

I want that string to turn into this:

Adam  is here

Basically, replace any single   but leave multiple ones untouched

Tried: (and naturally - no worky :)/> )

txt.replace(/( ){1}/g,'Adam  is here')
txt.replace(/[^]( ){1}[;]/g,'Adam  is here')



Any suggestions on how to accomplish this - I presume in regex?

This post has been edited by scottyadam: 11 December 2012 - 04:57 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Strip single html spaces in javascript

#2 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,329
  • Joined: 08-June 10

Re: Strip single html spaces in javascript

Posted 11 December 2012 - 05:21 PM

the best regexp I can offer is /\b \b/ though negative look-ahead would be better (though not supported).

PS. youíre using String.replace() wrongly.
Was This Post Helpful? 0
  • +
  • -

#3 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: Strip single html spaces in javascript

Posted 11 December 2012 - 05:31 PM

View PostDormilich, on 11 December 2012 - 05:21 PM, said:

PS. youíre using String.replace() wrongly.


Thanks for the response - works great...can elaborate on the above?
Was This Post Helpful? 0
  • +
  • -

#4 Dormilich  Icon User is online

  • 痛覚残留
  • member icon

Reputation: 3550
  • View blog
  • Posts: 10,329
  • Joined: 08-June 10

Re: Strip single html spaces in javascript

Posted 11 December 2012 - 05:33 PM

the second parameter is the string that replaces the match. you certainly donít want to replace   with your whole phrase Ö
Was This Post Helpful? 1
  • +
  • -

#5 scottyadam  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 116
  • Joined: 03-December 07

Re: Strip single html spaces in javascript

Posted 11 December 2012 - 05:47 PM

View PostDormilich, on 11 December 2012 - 05:33 PM, said:

the second parameter is the string that replaces the match. you certainly donít want to replace   with your whole phrase Ö


HA - I see what you mean - yes I meant to put ' ' instead of that string, I guess I'm too used to the PHP str_replace function :)/> where the haystack goes at the end.

This post has been edited by scottyadam: 11 December 2012 - 05:48 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1