How to split comma separated string using JavaScript? [duplicate] - javascript

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(',');

Related

Replace values from an array in string non case sensitive in javascript [duplicate]

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?"

How to give input to replace() when the string is present in an array [duplicate]

This question already has answers here:
Loop (for each) over an array in JavaScript
(40 answers)
Closed 3 years ago.
I have to replace the special characters in my string with underscore. replace() function is used to do it . I know that.
My string is "545123_Claims#Claims#Claims000117".
But the issue is that replace() accepts sting as input.
Actually my string is in an array like filArr= ["545123_Claims#Claims#Claims000117"].
So how can I replace the special character in thisstring which is inside an array?
You could map the replaced strings by taking a function.
const replacementFn = string => string.replace(/xxx/, 'yyy');
filArr = fillArr.map(replacementFn);

How to replace all / with - from string [duplicate]

This question already has answers here:
Replace forward slash "/ " character in JavaScript string?
(9 answers)
Why this javascript regex doesn't work?
(1 answer)
Closed 4 years ago.
I have a string field 01/01/1986 and I am using replace method to replace all occurrence of / with -
var test= '01/01/1986';
test.replace('//g','-')
but it does't give desire result. Any pointer would be helpful.
You just have a couple issues: don't put the regex in quotes. That turns it into a string instead of a regex and looks for that literal string. Then use \/ to escape the /:
var test= '01/01/1986';
console.log(test.replace(/\//g,'-'))
A quick way is to use split and join.
var test= '01/01/1986';
var result = test.split('/').join('-');
console.log(result);
Note too that you need to save the result. The original string itself will never be modified.

Spliting numbers out of a string contain characters and numbers [duplicate]

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/

Remove Substring in Javascript [duplicate]

This question already has answers here:
How to remove text from a string?
(16 answers)
Closed 7 years ago.
In JavaScript, I need to find a substring between &J= and Key using regex and remove it from my url which could contain several substrings.
Here is my url:
SID=18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003
Thanks
You can learn Regular Expression here and code for your problem is:
Here you go:
SID="18608202881669&Act=432&Mode=1&CLk=T&Key58=6003&dotnetdll=TopCoConfigurator.dll&dotnetfunc=CasingSizeSummary&SID=18608202881669&F=&J=CasingSize/CasingSizeSummary.asp&Key58=6003&ccs_casingsizeid=6003";
var re = /&J=(.*?)&Key/;
SID = SID.replace(re, '');
alert(SID);
See Fiddle

Categories