Efficient method to split a string in javascript [duplicate] - javascript

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?

Related

Find a specific value of a string between two chars/strings without knowing its value [duplicate]

This question already has answers here:
Get Substring between two characters using javascript
(24 answers)
Closed 2 years ago.
String example: "{something}" or "{word: something}"
What I need to do is get 'something', so the text between two specific parts of the string, in these case {-} and {word:-} (The 'something' part can change in every string, I never know what it is).
I tried using string.find() or regex but I didn't come up with a conclusion. What's the quickest and best way to do this?
What you need is a capture group inside a regex.
> const match = /\{([^}]+)\}/.exec('{foo}')
> match[1]
'foo'
The stuff in the parens, ([^}]+), matches any character but }, repeated at least once. The parens make it be captured; the first captured group is indexed as match[1].

Add a space after every character in a string JavaScript [duplicate]

This question already has answers here:
How to add spaces between every character in a string?
(4 answers)
Closed 3 years ago.
How can i convert a text like Test to T e s t I know It can probably be done with regex but i don't understand how
I've, Quite new to javascript, in python (Which i quite understand) It can be done with a for loop something like
print(" ".join(a for a in "Test"))
But join works differently in javascript and only works for arrays (lists) if i'm right
I've also tried using replace but it does nothing
console.log("Test".replace(""," "))
console.log("Test".replace(""," "))
"Test".split("").join(" ")
// or
[..."Test"].join(" ")
Thats it. You can't do that with .join directly as that only accepts a string.
JS doesn't support generator expressions, and join is an array method that takes the joiner not a string method that takes an iterable. The closest equivalent to your Python code would be
console.log(Array.from("Test").join(" "))
Using Array.from (converting the iterable string to an array) over .split("") has the advantage that it doesn't break unicode characters that consist of multiple code points apart.

How to filter a string data? [duplicate]

This question already has answers here:
Filter array by string length in javascript [duplicate]
(3 answers)
Closed 3 years ago.
I have a string for example:-
String: Notification 'Arcosa-Incident assigned to my group' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification's "Groups" field: 'Ruchika Jain' 9efa38ba0ff1310031a1e388b1050e3f
So basically i convert it into an array using .split(' ') method to make it comma separated values, now i want to filter this array and want only values which are 32 character long and remove rest of values.
Please help me achieve this. Alternate solutions are also welcomed. Thanks in advance.
Assuming you want to grab those IDs you can simply use a regex with match on the string without splitting/filtering it. (Note: I had to escape the single quotes in the text.)
const str = 'String: Notification \'Arcosa-Incident assigned to my group\' 8ff7afc6db05eb80bfc706e2ca96191f included recipients as manager of a group in the notification\'s "Groups" field: \'Ruchika Jain\' 9efa38ba0ff1310031a1e388b1050e3f';
const matches = str.match(/[a-f0-9]{32}/g);
console.log(matches);
Like so:
var arr = ...;
var filtered = arr.filter(word => word.length === 32);
Edit: this may be a bad idea if you want to parse only the GUIDs. It could certainly be that a name like "Ruchika" is also 32 characters long. Maybe, consider using regular expressions instead.

Split string on first comma into array [duplicate]

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).

Replace '-' with '--' in a JavaScript string [duplicate]

This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 4 years ago.
I am trying to replace a single dash '-' character in a string with double dashes.
2015–09–01T16:00:00.000Z
to be
2015-–09-–01T16:00:00.000Z
This is the code I am using but it doesn't seem to be working:
var temp = '2015–09–01T16:00:00.000Z'
temp.replace(/-/g,'--')
In JavaScript Strings are immutable. So, when you modify a string, a new string object will be created with the modification.
In your case, the replace has replaced the characters but returns a new string. You need to store that in a variable to use it.
For example,
var temp = '2015–09–01T16:00:00.000Z';
temp = temp.replace(/–/g,'--');
Note The string which you have shown in the question, when copied, I realised that it is a different character but looks similar to – and it is not the same as hyphen (-). The character codes for those characters are as follows
console.log('–'.charCodeAt(0));
// 8211: en dash
console.log('-'.charCodeAt(0));
// 45: hyphen
The hyphen character – you have in the string is different from the one you have in the RegExp -. Even though they look alike, they are different characters.
The correct RegExp in this case is temp.replace(/–/g,'--')
Probably the easiest thing would be to just use split and join.
var temp = '2015–09–01T16:00:00.000Z'.split("-").join("--");

Categories