Reputation: 6953
How to convert the space between letters into another character like "-"?
SOLVED:
For those who have the same problem:
var namePicked = names[Math.floor(Math.random() * names.length)]; var x = namePicked.replace(/[A-Za-z]/g, "_").replace(/\s/g, "-"); var y = x.split("").join(" ");
this will turn John Smith (randomly chosen) into
_ _ _ _ - _ _ _ _ _
Nicely spaced out by "spaces", letters become underscores and spaces become dashes. This is solved the "javascript" way. You can also do this with loop for.
Here's my code (before):
// list of names
var names = ["John Smith", "Joe Doe", "Ian Nobody", "James Black"];
var namePicked = names[Math.floor(Math.random() * names.length)];
var nameArray = new Array(namePicked.length);
window.onload = function() {
//convert chosen name to array and replace letters with dashes
for (var i = 0; i < nameArray.length; i++) {
nameArray[i] = "_";
}
//remove comma
var nameHidden = nameArray.join(" ");
document.getElementById("nameDisplay").innerHTML = nameHidden;
}
I can only turn John Smith into a line of 10 underscores, I want to make it 4 underscores
(first name) with a dash
(for the space) before the last 5 underscores
(last name). Also a space
between each underscore
. I'm sorry I didn't mention this earlier.
I've tried this:
for (var i = 0; i < nameArray.length; i++) {
if (nameArray[i] == " ") {
nameArray[i] = " ";
}
else {
nameArray[i] = "_";
}
}
And some other variations but none works.
Upvotes: 2
Views: 1627
Reputation: 3730
You can chain replace()
and split()
to replace all non-whitespace characters with an underscore, and then replace the space between first and last names with a dash.
var names = ["John Smith", "Joe Doe", "Ian Nobody", "James Black"];
var result = names.map(name => name.replace(/[^\s\\]/g, '_').split(' ').join('-'));
console.log(result);
If you want to space out the underscores, you can chain two more methods to this:
replace()
every underscore with an underscore and spaceslice()
- to remove the last spacevar names = ["John Smith", "Joe Doe", "Ian Nobody", "James Black"];
var result = names.map(name => name.replace(/[^\s\\]/g, '_').split(' ').join('- ').replace(/[\_]/g, '_ ').slice(0, -1));
console.log(result);
Upvotes: 3
Reputation: 6953
For those who has the same problem:
var namePicked = names[Math.floor(Math.random() * names.length)];
var x = namePicked.replace(/[A-Za-z]/g, "_").replace(/\s/g, "-");
var y = x.split("").join(" ");
this will turn John Smith (randomly chosen) into
_ _ _ _ - _ _ _ _ _
Nicely spaced out by "spaces", letters become underscores and spaces become dashes. This is solved the "javascript" way. You can also do this with loop for.
Upvotes: 0
Reputation: 158
You can use 'map' method. The map method will literally 'map' a function call onto each element in the array, so you need to create array from your string with method split().
var name = "John Smith";
var underscoredName = "";
name.split('').map(function(currentValue, index, nameString) {
if (currentValue != " ") {
underscoredName += "_";
} else {
underscoredName += "-";
}
});
console.log(name); // John Smith
console.log(underscoredName); // ____-_____
Upvotes: 1
Reputation: 9157
You can convert all spaces to any other character using the replace()
method:
var replaced = test.replace(/\s/g, '-');
Note that you have to assign the return value of replace
to a variable.
The /\s/g
regular expression selects all spaces.
Here's an example of how you could apply it to your situation:
var names = ["John Smith", "Joe Doe", "Ian Nobody", "James Black"];
console.log('names before: ', names);
for (var i = 0; i < names.length; i++) {
names[i] = names[i].replace(/\s/g, '-');
}
console.log('names after: ', names);
Upvotes: 0
Reputation: 504
Try this
<script>
var names = ["John Smith", "Joe Doe", "Ian Nobody", "James Black"];
var namePicked = names[Math.floor(Math.random() * names.length)];
window.onload = function () {
var nameoutput = "";
for (var i = 0; i < namePicked.length; i++) {
if (namePicked[i] == " ") {
nameoutput += " ";
}
else {
nameoutput += "X";
}
}
document.getElementById("nameDisplay").innerHTML = nameoutput;
}
</script>
Upvotes: 1
Reputation: 4991
How to convert the space between letters into another character like "-"?
Well you know what you want to replace, you know what you wanna replace it with, now you just need to use replace :
var new_str = old_str.replace(/ /g, "-");
The RegEx / /g
means "every single space"
Upvotes: 0