Reputation: 435
const text = 'hey : "bob " Hey Hello'
text.replace(' "', '"').replace('" ', '"')
// expect result 'hey :"bob"Hey Hello'
how to replace this whitespace before and after (")
Upvotes: 1
Views: 1053
Reputation: 2410
To stay close to your initial idea,
Where \s*\"
means any number of whitespaces before the "
symbol and \"\s*
means any number of whitespaces after the "
symbol. The g
after the regular expression is a flag which means global.
text.replace(/\s*\"/g, '"').replace(/\"\s*/g, '"')
Upvotes: 2
Reputation: 75656
You should be able to capture a substring like ABC
and remove leading and trailing whitespace with this regex.
test.replace(/\s*(ABC)\s*/g, $1);
Upvotes: 2