This question already has answers here:
split string only on first instance of specified character
(21 answers)
Closed 6 years ago.
I'm having a string like below that i would like to split only on the first ,. so that if i for instance had following string Football, tennis, basketball it would look like following array
["football", "tennis, basketball"]
This should do it
var array = "football, tennis, basketball".split(/, ?(.+)?/);
array = [array[0], array[1]];
console.log(array);
Inspiration: split string only on first instance of specified character
EDIT
I've actually found a way to reduce the above function to one line:
console.log("football, tennis, basketball".split(/, ?(.+)?/).filter(Boolean));
.filter(Boolean) is used to trim off the last element of the array (which is just an empty string).
Related
This question already has answers here:
Find the characters in a string which are not duplicated
(29 answers)
Closed 1 year ago.
I found many question about counting characters used in a string and solutions
but how can I get all character used in string? Im new to JavaScript, im do not know to output this
for example
"English-Language" to "E,n,g,l,i,s,h,-,L,a,u,e" or "Javascript String" to "J,a,v,s,c,r,i,p,t, ,S,n,g"
Thank you..
Very simple with Set: (just cast it back to an array)
console.log([...new Set("English-Language")]);
console.log([...new Set("Javascript String")]);
And if you want it as a single string, do this:
console.log([...new Set("English-Language")].join(","));
console.log([...new Set("Javascript String")].join(","));
You can use the split function and the Set class to achieve this like so.
new Set("Javascript String".split(''))
The split('') part splits the string using an empty string as the seperator, so it just returns an array of all the characters. If you make a set from that array, it'll remove the duplicates. You can even pass the string directly to the Set constructor. If you want it as an array, just use Array.from.
you can simply use str.split("");
const str = 'English-Language';
str.split("");
Let me know if you face any future issue.
or Second option is
console.log([...new Set("English-Language")]);
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);
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:
Closed 10 years ago.
Possible Duplicate:
Javascript code to parse CSV data
Javascript: Splitting a string by comma but ignoring commas in quotes
I have a comma separated string like this:
car,jeep,,"ute,van",suv,truck
I need to split it and add it to array as following entries:
car
jeep
ute,van
suv
truck
I am currently first splitting the string by " and then replace , with some other character in the parts that have , only in the middle but not at the either ends.
Also I check if the length of split array is greater than 1 because in case I get strings that don't have " at all, I want to skip the replacing , part.
Then finally I join the array using "" to get the end result.
Can someone suggest any better way and efficient way to do this possibly using regex?
This question already has answers here:
How do you access the matched groups in a JavaScript regular expression?
(23 answers)
Closed 6 years ago.
I have the following regex in javascript. I want to match a string so its product (,|x) quantity, each price. The following regex does this; however it returns an array with one element only. I want an array which returns the whole matched string first followed by product, quantity, each price.
// product,| [x] quantity, each price:
var pQe = inputText.match(/(^[a-zA-z -]+)(?:,|[ x ]?) (\d{0,3}), (?:each) ([£]\d+[.]?\d{0,2})/g);
(,|x) means followed by a comma or x, so it will match ice-cream, 99, $99 or ice-cream x 99, $99
How can I do that?
Use the exec function of the RegExp object. For that, you'll have to change the syntax to the following:
var pQe = /(^[a-z ... {0,2})/g.exec(inputText)
Element 1 of the returned array will contain the first element of the match, element 2 will contain the second, and so on. If the string doesn't match the regular expression, it returns null.