TimLee
TimLee

Reputation: 193

javascript replace all 


I have a sting like:

var tmp='Hello 
 I am 
   ( Peter';

I want to replace all words with this pattern &...; to '';

How I can change the code below to make it work:

tmp=String(tmp).replace(/
/g, "");

Thank you.

Upvotes: 1

Views: 400

Answers (2)

prasanth
prasanth

Reputation: 22500

try this /&#(\d+)(;)/g .Its match the numbers upto reach of ; end

Normal Regex

var tmp='Hello 
 I am 
     ( Peter';

console.log(tmp.replace(/&#(\d+)(;)/g,""))

Remove the Empty space also use: /(\s+)&#(\d+)(;)/g .\s+ match the empty space before the pattern

Empty space match

var tmp='Hello 
 I am 
     ( Peter';

console.log(tmp.replace(/(\s+)&#(\d+)(;)/g,""))

Upvotes: 1

Pranav C Balan
Pranav C Balan

Reputation: 115232

Update regex with \d{2} instead of the 10 to match any two digit numbers.

var tmp = 'Hello 
 I am 
   ( Peter';

tmp = String(tmp).replace(/&#\d{2};/g, "");

console.log(tmp);

Upvotes: 4

Related Questions