Regex Unexpected ^ and character match [duplicate] - javascript

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

Related

Unable to find patter in JS RegExp [duplicate]

This question already has answers here:
JavaScript regular expression "Nothing to repeat" error
(2 answers)
Why do regex constructors need to be double escaped?
(5 answers)
Closed 3 years ago.
I trying to identify regular expression in JS. Expression which wrote to identify the pattern is working in Online RegEx but when i use it in my own Code it does not work.
it says error .*|{1}/: Nothing to repeat
This is Expression -
Examples:\n.*\|{1}
Sample Data -
Examples:
|asda| asd|asd|adasda|adaasdsaasdasdsa|adsadsa|asdasdad|asdasdsd|asda |
Examples:
|Word| asd|asd|W#132|iam|GOto|asdasdad|asdasdsd|asda- |

invalid Regex group [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create the following regex using Javascript.
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
However, by doing this it gives me invalid group error in the console.
regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");
I don't understand where the problem comes from exactly. I appreciate the help.
Thank you
EDIT: After some research I found that Javascript does not support lookbehinds.
So the error comes from (?<!\\).
Refer this newly asked question to find an alternative way to do the same job.
How to check for odd numbers of backslashes in a regex using Javascript?
If your expression isn't dynamic, just use a literal:
var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;
The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.

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.

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

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