Javascript regex splitting words in a comma separated string
Use a negated character class:
/([^,]+)/g
will match groups of non-commas.
< a = 'hi,mr.007,bond,12:25PM'
> "hi,mr.007,bond,12:25PM"
< b=/([^,]+)/g
> /([^,]+)/g
< a.match(b)
> ["hi", "mr.007", "bond", "12:25PM"]
RegEx expression to split a string on comma with a condition in JavaScript
Assuming you only ever would have to worry about excluding commas inside singly-nested parentheses, we can try splitting on the following regex pattern:
\s*,\s*(?![^(]*\))
This says to split on comma, surrounded on both sides by optional whitespace, so long as we can't look forward and encounter a closing )
parenthesis without also first seeing an opening (
one. This logic rules out the comma inside c=add(3,b)
, because that particular comma hits a )
first without seeing the opening one (
.
var str = "a , b=10 , c=add(3,b) , d"parts = str.split(/\s*,\s*(?![^(]*\))/);console.log(parts);
REGEX: split javascript strings - ignore commas in nested strings and object-likes
You can use this regex to split on commas not under curley braces by using negative look ahead to ensure , is not contained in curley braces.
,(?![^{}]*\})
Demo, https://regex101.com/r/nGmRof/2
Here is a javascript code for demonstrating same.
var str = ['2,5,4','2,{"name": [2,3]}',' 2,{"key": "value", "key2":"value2" } '];for (i=0;i<str.length;i++) { console.log(str[i] + " --> split into following"); str[i] = str[i].split(/,(?![^{}]*\})/); for (j = 0; j < str[i].length; j++) { console.log(str[i][j]); } console.log('\n\n'); }
How I can split string by comma and/or new line and/or whitespace
You need to split on either a comma surrounded by some number of white-space characters, or just one or more white-space characters:
let strs = ['o@gmail.com b@gmail.com c@gmail.om','o@gmail.com, b@gmail.com,c@gmail.com','o@gmail.com,\n\ b@gmail.com, c@gmail.com'];
console.log(strs.map(s => s.split(/\s*,\s*|\s+/)))
Regex to split a comma separated css class names string into array
const arr = ' lmn-button ,lmn-button-primary'.trim().split(/\s*,\s*/);console.log(arr);
Split string by comma, but not escaped in JavaScript
So use match instead of split
"Keyword,slug,description".match(/([^,]+),([^,]+),(.*)/);
will result in
["Keyword,slug,description", "Keyword", "slug", "description"]
There are other ways to write the regular expression, just picked something quick.
Related Topics
Angular - How to Get Result of a Json Object
Change HTML Code Without Refreshing the Page
Add Hyphen After Every Fourth Character in a String Seperated by Comma
Change Specific Button Text on Click Inside Ngfor
Save Only Date Without Timezone JavaScript
How to Change Iframe Src Without Reloading the Iframe
Get Docx File Contents Using Javascript/Jquery
Get Request Works With Postman But Why Doesn't It Work With Reactjs Fetch
Javascript: How to Read a Hand Held Barcode Scanner Best
Google Charts: Move Legend Position
Check Checkbox and Trigger Change Event JavaScript
Check If an Array Is Empty in React Native
Prevent Html5 Videos from Downloading the Files on Mobile - Videojs
Resize a Base-64 Image in JavaScript Without Using Canvas
How to Fetch Url of Current Tab in My Chrome Extension Using JavaScript
Es6/Babel Class Constructor Cannot Be Invoked Without 'New'