Split by word not contained in parentheses [duplicate] - javascript

This question already has answers here:
How to split by commas that are not within parentheses?
(6 answers)
How to split string while ignoring portion in parentheses?
(5 answers)
Closed 3 years ago.
Here is the my string
var string = 'Title:(India OR America) OR Keyword:(Education)';
After splitting with "OR" it is showing
["Title:(India", "America)", "Keyword:(Education)"]
But what I need is the below one.
["Title:(India OR America)", "Keyword:(Education)"]
Could anyone please help on how to split the string to get the required result.

To achieve this you'll need to use a negative lookahead to only find the OR string where it's not contained in parentheses. Try this:
var input = 'Title:(India OR America) OR Keyword:(Education)';
var output = input.split(/(?!\(.*)\s?OR\s?(?![^(]*?\))/g);
console.log(output);

Related

Handling special characters in Java script to enclose them in Square Brackets? [duplicate]

This question already has answers here:
Wrap Numbers in Brackets using javascript .replace regex
(1 answer)
Check for special characters in string
(12 answers)
Closed 2 years ago.
I am trying to write a Java Script function that should enclose all the special characters in the given String in a bracket like following -
Input ===> Output
123##$, ====> 123[#][#][$][,]
Changing All the special characters to certain String [ _ in this case] is easy -
string = string.replace(/[^a-zA-Z0-9]/g,'_');

Regex to get string between two specific tags [duplicate]

This question already has answers here:
Regular expression to get a string between two strings in Javascript
(13 answers)
What special characters must be escaped in regular expressions?
(13 answers)
Closed 4 years ago.
I have a string like:
[device]Some device[/device]
I am trying to write regex that will return only "Some device".
/[device](.*?)[/device]/i.exec(str) // doesn't work
/[device](.*)[\/device]/g.exec(text) // doesn't work
I am not sure how to extract it?
I already tried solution from Regular Expression to get a string between two strings in Javascript and it doesn't work for string like [][/]
[device]Some device[/device]
return
evice]Some device[/device] and all other strings.

Javascript string split with regex multiple conditions [duplicate]

This question already has answers here:
Javascript: Split a string by comma, except inside parentheses
(7 answers)
Regex split string by spaces taking into account nested brackets
(2 answers)
Closed 5 years ago.
I'm trying to split string
x#x,a,b,c,d(some(other,arg2),p)#yyy into array
['x#x','a','b','c','d(some(other,arg2),p)#yyy'].
The problem i faced is that i cannot split just comma /\,/g since members that contains already commas would split also. Any ideas?

REGEX javascript - find two chars set [duplicate]

This question already has answers here:
What special characters must be escaped in regular expressions?
(13 answers)
Closed 5 years ago.
i've to find the sequence of chars ,. inside a json file for example: ":0,.0}},{", so what is the best fitted regex string for it?
i tried /(,.){1,}/g but it doesn't work well.
It's because . is a special character.
const r = /(?:,\.)+/g
console.log('abc,.,.,.def'.match(r)) //[",.,.,."]
you need to escape the .:
/,\./g

Javascript Regular Expression to match six-digit number [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I am trying to incorporate a regular expression i have used in the past in a different manner into some validation checking through JavaScript.
The following is my script:
var regOrderNo = new RegExp("\d{6}");
var order_no = $("input[name='txtordernumber']").val();
alert(regOrderNo.test(order_no));
Why would this not come back with true if the txtordernumber text box value was a six digit number or more?
You have to escape your \ when used inside a string.
new RegExp("\\d{6}");
or
/\d{6}/
Insert an extra "\" in your regexp.
You need to escape your backslash. It's looking for "\d", not digits.
So...
var regOrderNo = new RegExp("\\d{6}");

Categories

Resources