PsychoX
PsychoX

Reputation: 1098

Regular Expression replace ' - ' with '-'

I'm not sure if this is a good place to ask questions like this, but I'll give a try :)

I need help with RegExp. I'm trying to write expresion which will replace ['any number of spaces' + '-' + 'any number of spaces'] and replace it simply with '-'. So it's all about removing spaces if in the middle is placed '-'.

So for example:

'something -  something' -> 'something-something'

Upvotes: 0

Views: 72

Answers (1)

Quentin
Quentin

Reputation: 943097

Any number is * so just:

/ *- */-/

PHP:

preg_replace("/ *- */", "-", "fooo   -    bar ");

JS:

"fooo   -    bar ".replace(/ *- */, "-");

Upvotes: 2

Related Questions