-1

I found a thread in the c# forums that's exactly what I need, but I need it in JavaScript. Basically I need regex that will put spaces between each character of a phrase.

For example,

TEST

would become

T E S T

Explanations would be helpful, and I'm new to this, so please be nice :)

I'm building my project in a program that only allows regex code, so it needs to be that.

5
  • This is me nicely asking you what you've tried so far and why you want to use a regex for this. You can get by without one by splitting the string into characters then joining them with a space. str.split("").join(" "); Commented May 16, 2015 at 19:20
  • 1
    I'm voting to close this question as off-topic because it uses offensive language. Commented May 16, 2015 at 19:27
  • What would T E S T A become ? Commented May 16, 2015 at 19:32
  • What kind of "program" would you be building your project in that only allows regex code? Regexps themselves do not manipulate or mutate strings; they only match. You can use routines such as String#replace to mutate a string based on the results of matching a regexp. However, if you are able to use replace, then you are also able to use split and join. Commented May 16, 2015 at 19:37
  • 1
    @torazaburo I'm making a translator at lingojam.com, and i have literally never coded before. im about ready to drop the question and give up, because, to no one's surprise, the internet responds to ignorance with rude messages. Commented May 16, 2015 at 19:41

3 Answers 3

9
"TEST".replace(/(.)(?=.)/g, "$1 ")
// Outputs
// => T E S T
  • (.) Matches a single character. Captures in group 1($1)

  • (?=.) Positive look ahead. Checks if the captured character is followed by another character.

  • "$1 " Replacement string. $1 Contains the character captured in group 1, followed by a space

  • g Global modifier. Applies the replace globally for all the matches within the string.

Regex Demo

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

Comments

2

You can use the following:

(.)(?!$)

And replace with '$1 '(space)

See DEMO

Comments

0

You say you want this in JavaScript, then you edited your question (after I posted this answer) to say you must use a regex. Without that (strange, to me) requirement, you can do this with split() and join() in JavaScript without using a regex.

var myString = "TEST";
var result = myString.split('').join(' ');
console.log(result); // "T E S T"

For this and all the regular expression solutions so far provide, if you will have special characters, this will be problematic. For example 'foo 𝌆 bar'.split('').join(' '); returns an unexpected result. So does 'foo 𝌆 bar'.replace(/(.)(?=.)/g, "$1 "); and 'foo 𝌆 bar'.replace(/(.)(?!$)/g, "$1 "); as well. But if you know you are dealing with (say) ASCII-safe text, then there won't be any problems.

6 Comments

There are no "multi-byte" characters in JS strings. There are just Unicode characters. Split and join will work fine on any characters.
@torazaburo I was thinking of the problem described at github.com/mathiasbynens/…. I think you're right, though, that this will not apply here.
Oops, I take it back. There absolutely are problems with this and unusual characters. For example, try this in your browser console: 'foo 𝌆 bar'.split('').join(' ');
What is that character?
|

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.