REGEX javascript - find two chars set [duplicate] - javascript

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

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,'_');

Split by word not contained in parentheses [duplicate]

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

Regex Unexpected ^ and character match [duplicate]

This question already has answers here:
Javascript: Split a string into array matching parameters
(3 answers)
Closed 4 years ago.
I have this regular expression that tokenizes calculator input strings like 12+3.4x5 to 12,+,3.4,x,5
The regular expression used is
\d+\.?\d+|[\+-÷x]
I get an unexpected match with ^ and letters.
Regex solution, although there's probably a cleaner way of writing this if someone could point it out?
(\d+\.?\d+|\d+|(\+|-|÷|x))

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.

How can I use Regular Expression to match Chinese characters like ,。‘;’ [duplicate]

This question already has answers here:
Regular expression to match non-ASCII characters?
(8 answers)
Closed 5 years ago.
I'm using javascript to convert string to what I want!!
Can I use Regular Expression and use what Regular Expression??
Is there anyone that can help me?
First, you need to get corresponding Unicode code for these Chinese characters. Here is a helpful tool.
character code(base 10) code(hex)
, 65307 0xff1b
。 65292 0xff0c
; 12290 0x3002
Second, use these Unicode code to form regexp.
js:
/[\u3002\uff0c\uff1b]/.test('A string contains Chinese character。')
true

Categories

Resources