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 bee
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.
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 bee
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.
Share Improve this question edited Jun 20, 2020 at 9:12 CommunityBot 11 silver badge asked May 16, 2015 at 19:18 deactivated874569854679402deactivated874569854679402 331 silver badge4 bronze badges 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(" ");
– user1106925 Commented May 16, 2015 at 19:20 - 1 I'm voting to close this question as off-topic because it uses offensive language. – user663031 Commented May 16, 2015 at 19:27
-
What would
T E S T A
bee ? – user557597 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 usereplace
, then you are also able to usesplit
andjoin
. – user663031 Commented May 16, 2015 at 19:37 - 1 @torazaburo I'm making a translator at lingojam., 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 Commented May 16, 2015 at 19:41
3 Answers
Reset to default 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 spaceg
Global modifier. Applies the replace globally for all the matches within the string.
Regex Demo
You can use the following:
(.)(?!$)
And replace with '$1 '
(space)
See DEMO
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