Javascript Replace all commas not in double quotes [duplicate] - javascript

This question already has answers here:
Regex to match all instances not inside quotes
(4 answers)
Closed 3 years ago.
I would like to replace all commas in a comma-delimited string with a pipe ('|') except for those that are found in double quotes. I would prefer to use the JavaScript "replace" function if possible.
My regex knowledge is limited at best. I am able to replace all commas with pipes, but that does not give me the desired result for parsing through the data. I also found a regex on here that removed all commas except those in quotations, but does not implement a pipe or some other delimiter.
(?!\B"[^"]*),(?![^"]*"\B)
Here is an example of what I'm trying to accomplish:
string1 = 1234,Cake,,"Smith,John",,"Status: Acknowledge,Accept",,Red,,
and I would like it to look like:
string1 = 1234|Cake||"Smith,John"||"Status: Ackknowledge,Accept"||Red||

One option is to use a replace callback to replace either a quote or a comma with the quote itself or a pipe respectively:
str = `1234,Cake,,"Smith,John",,"Status: Acknowledge,Accept",,Red,,`;
res = str.replace(/(".*?")|,/g, (...m) => m[1] || '|');
console.log(res)
Another (and IMO better in the long run) would be to use a dedicated parser to work with CSV data. CSV is actually trickier than it looks.

We can simply capture our desired commas using alternation with a simple expression such as:
(".+?")|(,)
Demo
RegEx Circuit
jex.im visualizes regular expressions:

Related

Regex non-greedy and global modifiers not working as expected with JavaScript .match [duplicate]

This question already has answers here:
Javascript regular expressions modifier U [duplicate]
(3 answers)
Closed 5 months ago.
I'm trying to create a Regex that will return text that is wrapped by parentheses. For example, in the following string combination:
const regexString = "asdf (asdfasd asdfas) asdfasd asdfasd asdf(asfda) asdfasd (asdfasd)"
the regex should return only: (asdfasd asdfas), (asfda), and (asdfasd) as individual capture groups.
Using regex101.com I was able to put this combination together:
/(\(.+\))/gU
This regex combo works, but when I try to implement this in Javascript .match or even with .exec, I am simply returned the entire string.
For example,
regexString.match(/(\(.+\).*?)/g)
returns the entire string.
I believe the issue has to do my use of the ungreedy .*? modifier and the global /g modifier. Both of these are used in the working example from regex101.com, but I haven't been able to determine exactly why these modifiers or possibly the regex are not functioning the same when I try to use them in Javascript directly.
Thank you for any insight!
I believe you dont get entire string, but by using greedy modifier you get all characters between first opening and last closing parentheses. In your example the returned value is array with single string:
['(asdfasd asdfas) asdfasd asdfasd asdf(asfda) asdfasd (asdfasd)']
You need to change your regex with nongreedy ? to get least possible amount of characters between parentheses
regexString.match(/(\(.+?\).*?)/g)
Then the returned result will be:
['(asdfasd asdfas)', '(asfda)', '(asdfasd)']
what you're searching for is /\([^)]*\)/g
\( : will match the opening parenthese
[^)] : will match any non closing parenthese
* : will match many times the last character
\) : will match a closing parenthese

Defining regex in Javascript [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 1 year ago.
I need to define below regex in Javascript:
(\bin region\b)(?!.*\1).+?(?=<)
I tried like below but it looks like not working:
var reg = new RegExp ('(\bin region\b)(?!.*\1).+?(?=<)');
Although the Atom tool matches the regex in the target string, JavaScript code is returning blank.
I am using this in Azure logic app (inline code executor connector)
Anyone can help me with this?
You can see it matches the text in Atom:
Inside a string you need to escape the backslashes.
Otherwise the backslash will escape the next character. So, writing \b will escape the character b instead use \\b which will escape the \
var reg = new RegExp ('(\\bin region\\b)(?!.*\\1).+?(?=<)');

How to ignore brackets in a regex [duplicate]

This question already has an answer here:
Escape string for use in Javascript regex [duplicate]
(1 answer)
Closed 3 years ago.
I have a regex that takes a template literal and then matches it against a CSV of conditions and links.
const regex = new RegExp(`^${condition},\/.+`, 'gi');
For example, the variable Sore throat would match
'Sore throat,/conditions/sore-throat/'
I've come across an issue where the template literal might contain brackets and therefore the regex no longer matches. So Diabetes (type 1) doesn't match
'Diabetes (type 1),/conditions/type-1-diabetes/'
I've tried removing the brackets and it's contents from the template literal but there are some cases where the brackets aren't always at the end of the string. Such as, Lactate dehydrogenase (LDH) test
'Lactate dehydrogenase (LDH) test,/conditions/ldh-test/'
I'm not too familiar with regex so apologies if this is simple but I haven't been able to find a way to escape the brackets without knowing exactly where they will be in the string, which in my case isn't possible.
You are trying to use a variable that might contain special characters as part of a regex string, but you /don't/ want those special characters to be interpreted using their "regex" meaning. I'm not aware of any native way to do this in Javascript regex - in Perl, you would use \Q${condition}\E, but that doesn't seem to be supported.
Instead, you should escape your condition variable before passing it into the regex, using a function like this one:
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&'); // $& means the whole matched string
}

Workaround for negative look-behind in javascript [duplicate]

This question already has answers here:
javascript regex - look behind alternative?
(8 answers)
Closed 6 years ago.
I'm converting a python script I wrote to javascript. My python script has the following regex to match single instances of '\':
re.sub(r'(?<!\\)(\\{1})(?!\\)', r'\\', word)
I got a compiler error when trying to run this in js:
"Invalid regular expression: /(?<!\\)(\\{1})(?!\\)/: Invalid group"
After some searching found out that regex in js does not support look behinds.
I looked at this answer and they used:
^(?!filename).+\.js
in the form of a negative look-ahead from the start of the string, which does not help me as I need to change '\' to '\\' anywhere in the string.
I do not think this is a duplicate question as my question is trying to determine how to avoid and match the same character at different points in a string, while the linked question seeks to avoid a specific phrase from being matched.
I need to match '\' characters that do not have '\' either before or after them.
You always can use capture groups instead of lookbehind
string.match(/(^|[^\\])(\\{1})(?!\\)/)[2]
let replaced = "a\\b\\\\".replace(/(^|[^\\])(\\{1})(?!\\)/, x => x[0] == '\\' ? x : 'value')
console.log(replaced)
will return you same thing as (?<!\\)(\\{1})(?!\\)
Just match without assertions (^|[^\\])\\([^\\]|$) then substitute them back.
Note that this will tell you nothing about if it is escaping anything or not.
That regex is more complex.

Replace pattern within string with space [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 7 years ago.
I am getting long string with multiple occurances of pattern './.'. The string has dates as well in a format of dd.mm.yyyy.
First I tried with javascript replace method as:
str.replace('./.', ''). But it replaced only first occurance of './.'
Then I tried another regex which replaces special characters but it didn't work as it replaced '.' within dates as well.
How do I replace multiple occurances of a pattern './.' without affecting any other characters of a string ?
. is a special character in a regexp, it matches any character, you have to escape it.
str.replace(/\.\/\./g, '');
Use this simple pattern:
/\.\/\./g
to find all "./." strings in your text.
Try it :
str.replace(/\.\/\./g, '');
Escape . and d \
Add a g for global
Like this
str = str.replace(/\./\./g, '');

Categories