Ahmed Elsayed
Ahmed Elsayed

Reputation: 435

how remove white space after and before specific character

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

Answers (2)

Nathan Furnal
Nathan Furnal

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

chovy
chovy

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

Related Questions