Split string on newline and comma [duplicate] - javascript

This question already has answers here:
How do I split a string with multiple separators in JavaScript?
(25 answers)
Closed 7 years ago.
My input String is like
abc,def,wer,str
Currently its splitting only on comma but in future it will contain both comma and newline.
Current code as below:
$scope.memArray = $scope.memberList.split(",");
In future I need to split on both comma and newline what should be the regex to split both on comma and newline.
I tried - /,\n\ but its not working.

You can use a regex:
var splitted = "a\nb,c,d,e\nf".split(/[\n,]/);
document.write(JSON.stringify(splitted));
Explanation: [...] defines a "character class", which means any character from those in the brackets.
p.s. splitted is grammatically incorrect. Who cares if it's descriptive though?

You could replace all the newlines with a comma before splitting.
$scope.memberList.replace(/\n/g, ",").split(",")

Try
.split(/[\n,]+/)
this regex should work.

Related

Removing all occurrences of '^' from a String [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 2 years ago.
I have this sorta string = "^My Name Is Robert.^"
I want to remove the occurrences of ^ from this string. I tried the replace method like :
replyText.replace(/^/g, '');
But it hasn't any affect. Using the replace without the global works but only removes the first occurrence.
Should I just make a loop and keep looping the string with replace till no more '^' are contained, or is there a better way?
You need to escape the ^ character in RegEx:
replyText.replace(/\^/g, '');
The caret, ^, is a special character in Regex, therefore it has to be escaped with a backslash i.e
replyText.replace(/\^/g, '')

how to exclude single quotes from the reg expression /\W+/ in javascript? [duplicate]

This question already has an answer here:
How can I exclude some characters from a class?
(1 answer)
Closed 3 years ago.
I want to split a paragraph of text using regex, but /\W+/ splits can't into can and t.
Is there any way to define a regular expression that splits whenever a character other than a-z,A-Z,0-9 and '(single quotes) is encountered?
text=text.split(/\W+/);
The following regex splits on everything you asked for , that is a-z, A-Z, 0-9, and ' :
text=text.split(/[^a-zA-Z0-9\']/);

Multiple conditions in regex - how to replace both colon and space with dash [duplicate]

This question already has answers here:
Replace multiple characters in one replace call
(21 answers)
Closed 3 years ago.
How to replace both colon and space with dash in regex?
Here's what I've managed to do:
to replace space: replace(/\s+/g, '-'),
to replace colon: replace(/:\s+/g, '-').
How do I merge these expressions?
You could do something like this:
var text = "hello: hey"
console.log(text.replace(/(:|\s+)/g, "-"))
Returns "hello--hey"
Use an alternation [ :]
var input = "Hello World:Goodbye";
console.log(input);
input = input.replace(/[ :]+/g, '-');
console.log(input);
Note that this replaces actual spaces, not all whitespace characters, which your original version using \s does.

Javascript Regex multi space "\s" doesn't work in chrome [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 6 years ago.
I try to use this Regex
new RegExp('utm_source=Weekly\SRecommended\SJobs\SEmail', 'ig');
When I try to use it in regex101 or regexr it works.
And in my code doesn't work.
I try to use this in console this is the result is
/utm_source=WeeklysRecommendedsJobssEmail/gi
the code without spaces.
When I try to use space letter it works.
Any help?
Because \ is an escape character for regular expressions and strings. You have to escape the \ if you're creating a regex from a string:
new RegExp('utm_source=Weekly\\SRecommended\\SJobs\\SEmail', 'ig');
Or simply use a regex literal which exists precisely to avoid this problem:
/utm_source=Weekly\SRecommended\SJobs\SEmail/ig

Matching content within a string with one regex [duplicate]

This question already has answers here:
How do you use a variable in a regular expression?
(27 answers)
Simple way to use variables in regex
(3 answers)
Closed 10 years ago.
I am looking for a way to RegEx match for a string (double quote followed by one or more letter, digit, or space followed by another double quote). For example, if the input was var s = "\"this is a string\"", I would like to create a RegEx to match this string and produce a result of [""this is a string""].
Thank you!
Use the RegExp constructor function.
var s = "this is a string";
var re = new RegExp(s);
Note that you may need to quote the input string.
This should do what you need.
s =~ /"[^"]*"/
The regex matches a double quote, followed by some number of non-quotes, followed by a quote. You'll run into problems if your string has a quote in it, like this:
var s = "\"I love you,\" she said"
Then you'll need something a bit more complicated like this:
s =~ /"([^"]|\\")*"/
I just needed a pattern to match a double quote followed by one or more characters(letters, digits, spaces) followed by another double quote so this did it for me:
/"[^"]*"/

Categories

Resources