-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.

Community
  • 1
  • 1
  • 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(" ");` –  May 16 '15 at 19:20
  • 1
    I'm voting to close this question as off-topic because it uses offensive language. –  May 16 '15 at 19:27
  • What would `T E S T A` become ? –  May 16 '15 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`. –  May 16 '15 at 19:37
  • 1
    @torazaburo I'm making a translator at http://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. – deactivated874569854679402 May 16 '15 at 19:41

3 Answers3

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

nu11p01n73R
  • 26,397
  • 3
  • 39
  • 52
2

You can use the following:

(.)(?!$)

And replace with '$1 '(space)

See DEMO

karthik manchala
  • 13,492
  • 1
  • 31
  • 55
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.

Trott
  • 66,479
  • 23
  • 173
  • 212
  • There are no "multi-byte" characters in JS strings. There are just Unicode characters. Split and join will work fine on any characters. –  May 16 '15 at 19:25
  • @torazaburo I was thinking of the problem described at https://github.com/mathiasbynens/esrever#why-not-just-use-stringsplitreversejoin. I think you're right, though, that this will not apply here. – Trott May 16 '15 at 19:30
  • 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(' ');` – Trott May 16 '15 at 19:32
  • What is that character? –  May 16 '15 at 19:34
  • It is this Unicode character: http://www.fileformat.info/info/unicode/char/1D306/index.htm – Trott May 16 '15 at 19:41
  • It's a character on a secondary plane, which is not going to work very well in any context. –  May 16 '15 at 19:44