1

Whatever string is given I have to see if there is exactly one space after and before =, If it is more than one space in either side I have to reduce that to one and if there is none, I have to insert one.

How should I do that ? String can contain anything.

Thanks

2
  • 1
    Good answers below. You might also considering using /\s*=\s*/gm in case you want all whitespace characters to be converted to single space characters in your expression Commented May 13, 2010 at 15:37
  • Thanks all, all the answers are right. Can give only one answer the right sign :( Commented May 13, 2010 at 16:27

4 Answers 4

2

You can do this:

str = str.replace(/ *= */g, " = ");

This will replace all = characters regardless of how many spaces it is surrounded by. The * quantifier will match as most spaces as possible while allowing even no spaces at all.

Sign up to request clarification or add additional context in comments.

Comments

1

Try this:

var out = in.replace(/ *= */g, " = ");

Basically just replace zero or more instances of a space with a space and you get both desired results. If zero, then you get one. If more than one, you get one.

Comments

1

Make the following replacement:

s = s.replace(/ *= */g, ' = ')

Comments

1
myString.replace(/\s*=\s*/g, " = ")

will do the same as other given answers, but allow any type of space characters to be replaced (spaces, tabs, etc.).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.