user3844085
user3844085

Reputation: 23

Regular expression for find and replace double “NON BREAKING SPACE”

I want to find with jQuery or Javascript the way to detect exactly the occurrence of two consecutive non-breaking spaces.

text = text.replace(/  /g, "");

text = text.replace(/ {2}/g, "");

For instance this is correct for me :

HELLO  WORLD

But this lines didn't work

but no HELLO    WORLD

Can somebody help me?

Upvotes: 2

Views: 3584

Answers (2)

Bhojendra Rauniyar
Bhojendra Rauniyar

Reputation: 85575

You can use just like this to replace space only coming from  

text.replace(/ /g, "");

No need to use two times   as g is for global search.


And this will allow you to remove any type of unwanted space:

text.replace(/\s/g, "");

Upvotes: 0

anubhava
anubhava

Reputation: 785481

You can use:

var repl = text.replace(/( ){2}/g, "");
//=> HELLOWORLD

Your regex: / {2}/ is effectively trying to match  ;

To remove all instances of   use:

var repl = text.replace(/( )+/g, "");

Upvotes: 1

Related Questions