0

I thought I'd space out after every letter in a string, for example foo becomes f o o

What I'm thinking is 'foo'.replace(//g, ' '); ( the g flag to replace every instance, otherwise I wouldn't want regex, would I :) ).

I only have one slight problem... in JavaScript, // is a comment, so it does not work.

How can I achieve this typing a regex literal (with slashes), or is it not possible and I would have to create (by typing) a Regex object?

3
  • /(?:)/g, going by new RegExp('', 'g').toString(). Commented Jan 21, 2014 at 23:14
  • @JonathanLonowski That would work, but /()/g is simpler because there's no capturing going on here. (Which is what I posted in my answer) Commented Jan 21, 2014 at 23:15
  • @DoorknobofSnow Sure. But, I wasn't mentioning it to compete. Just noting it as a common definition in various engines for an "empty" pattern. Commented Jan 21, 2014 at 23:32

3 Answers 3

2

Try this regex instead:

/.(?!$)/g

You can read it as: "Find all char except the last one."

Description

Regular expression visualization

Sample code

'foo'.replace(/.(?!$)/g, '$& ');

Demo

http://regex101.com/r/xH9bK1

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

Comments

1

There are a few ways you could do this. (These methods are ranked from best practice to worst. I recommend the first one, and I highly discourage the last.)


Use split/join, which would be a better method with less overhead. No need for a regex here:

'foo'.split('').join(' ');

Or use the RegExp constructor

'foo'.replace(new RegExp('', 'g'), ' ');

Or, add a useless group (not recommended because it's unclear at first glance):

'foo'.replace(/()/g, ' ');

One more comment, based on your last paragraph:

How can I achieve this using a regex literal, or is it not possible and I would have to create a Regex object?

They are literally the same thing:

screenshot

"using a regex literal... or... Regex object" doesn't make sense, because a regex literal is a RegExp object.

4 Comments

The regex might be faster in some browsers, here's a useless test jsperf.com/split-join-vs-regex-literal-replace
The useless group approach will add a space before and after the string.See here: regex101.com/r/pT9mT5
@Alex It's never mentioned that's unwanted... in fact, that's what would happen if you could use // as a regex literal.
@DoorknobofSnow OP states on the very first line (at the end): for example foo becomes f o o
0

You could use the \B (everything but word boundary)

'foo'.replace(/\B/g,' ');

or if you want to replace at every place

'foo'.replace(/\B|\b/g,' ');

1 Comment

this will change, for example, foo bar to f o o b a r (instead of the correct result, f o o [space] b a r). Also it's even more unclear and hard to understand. :P

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.