This question already has an answer here:
array join() method without a separator
(1 answer)
Closed 3 years ago.
I need to get entire array result as a clear string like: namegenderage, but can't figure out how
I tried:
var arr = ["name","gender","age"];
var string = arr.toString().replace(",","");
console.log(string);
log result should be string like namegenderage without quotes and brackets but I get this result "namegender,age"
You can just use the join() method:
var arr = ["name","gender","age"];
var string = arr.join('');
console.log(string);
Related
This question already has answers here:
Case insensitive replace all
(7 answers)
Closed 2 years ago.
My code is as follows:
array.forEach(el => {
string = string.replace(el, `censored`);
});
array : my array of words that I want to censor.
string : the string that the words need censoring.
My issue is that this process is quite slow and also if the word in my string is written using capitals, it's getting missed.
Any ideas how should I solve this issue?
Thank you.
maybe you can use regex
let array = ['mate']
let string = 'Hello Mate, how are you mate?'
let re = new RegExp(array.join("|"),"gi");
let str = string.replace(re, 'censored');
output:
"Hello censored, how are you censored?"
This question already has answers here:
How to split newline
(13 answers)
Closed 3 years ago.
I have a string with multiple lines.
one
two
three
I need to add them to an array separated by line
How can I do that?
You can use string.split to cut your string at newlines. You can add a Array.filter to remove the empty lines.
Filter will loop at every string and create a new array. If the string is empty it will not push it to the new array.
const str = `one
two
three`;
const ret = str.split('\n').filter(x => x.length);
console.log(str);
console.log(ret);
You can use String.prototype.split() such as below
const someString = `one
two
three`;
const myArray = someString.split('\n');
console.log(myArray);
This question already has answers here:
Extract numbers from a string using javascript
(4 answers)
Closed 6 years ago.
I want to split the numbers out of a string and put them in an array using Regex.
For example, I have a string
23a43b3843c9293k234nm5g%>and using regex I need to get [23,43,3843,9293,234,5]
in an array
how can i achieve this?
Use String.prototype.match()
The match() method retrieves the matches when matching a string against a regular expression
Edit: As suggested by Tushar, Use Array.prototype.map and argument as Number to cast it as Number.
Try this:
var exp = /[0-9]+/g;
var input = "23a43b3843c9293k234nm5g%>";
var op = input.match(exp).map(Number);
console.log(op);
var text = "23a43b3843c9293k234nm5g%>";
var regex = /(\d+)/g;
alert(text.match(regex));
You get a match object with all of your numbers.
The script above correctly alerts 23,43,3843,9293,234,5.
see Fiddle http://jsfiddle.net/5WJ9v/307/
This question already has answers here:
Get string inside parentheses, removing parentheses, with regex
(3 answers)
Closed 9 years ago.
I am trying to extract a string from within a larger string where i need to get the value only inside the brackets.
var str = 'ajay kumar (68766)';
Try this:
var str = 'ajay kumar (68766)';
str = str.slice(str.indexOf('(')+1, str.indexOf(')'));
How about using a regular expression?
var str = 'ajay kumar (68766)';
var output = str.replace(/^[\s\S]*?\((\d+)\)[\s\S]*?$/, '$1');
This question already has answers here:
How do I split a string, breaking at a particular character?
(17 answers)
Closed 3 years ago.
I want to split a comma separated string with JavaScript. How?
var partsOfStr = str.split(',');
split()
var array = string.split(',')
and good morning, too, since I have to type 30 chars ...
var result;
result = "1,2,3".split(",");
console.log(result);
More info on W3Schools describing the String Split function.
Use
YourCommaSeparatedString.split(',');