Regex - unhandled js exception in react native - javascript

I am using the following regex in my react-native app.
This is an email validation regex :
^[\w]+#((?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+)(?:[A-Za-z0-9-]{2,63}(?<!-))
This works fine in the browser but crashes the react native app due to the following :
no stack', reason: 'Unhandled JS Exception: Invalid regular expression: invalid group specifier name
Could someone please help getting this to work on react native, maybe by achieving the same thing that this regex achieves but without the lookbehind expression ?

The problem is likely caused the by the (?<!-) negative lookahead at the end of the regex pattern, which your JavaScript engine does not support. To ensure that a hyphen does not occur at the end of the email, we can simply use:
^[\w]+#((?:[A-Za-z0-9](?:[A-Za-z0-9-]{0,61}[A-Za-z0-9])?\.)+)(?:[A-Za-z0-9-]{1,62}[A-Za-z0-9])
That is, just use [A-Za-z0-9] to represent the final of 63 possible characters in the pattern.

This regex will not work likely problem in regex pattern i think.
this works for me for simple email format abc#mail.com
var regex = /^(([^<>()\[\]\\.,;:\s#"]+(\.[^<>()\[\]\\.,;:\s#"]+)*)|(".+"))#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;

Related

REGEX working in chrome but breaks in safari

The following javascript regex breaks in safari getting SyntaxError: Invalid regular expression: invalid group specifier name"
/^(?!\s)[A-Za-z0-9\'\.\-\,\s]*(?<!\s)$/.test('ABCD##');
Can someone please help me to re write the regex which can work in safari?
I find out that safari doesn't support lookbehind but still not able to re write the whole regex which can be good for safari.
Modify your pattern to avoid the negative lookbehind. Since you seem to want a non whitespace character as the last character, just use a character class for that.
/^(?!\s)[A-Za-z0-9'.,\s-]*[A-Za-z0-9'.,-]$/.test('ABCD##')
Side note: your current pattern looks wrong for what you are trying to match.

? preceding token is not quantifiable

I've got this regex which doesn't give any problems in the chrome browser but that crashes the storybook on safari with this error:
Invalid regular expression: invalid group specifier name
When I put my regex in the regex101.com editor, it gave me no errors (in the PHP section) but when I switched it to javascript i got this error:
? The preceding token is not quantifiable
regex: /^(.*)(?<=(#))([^# ])*(<br>)?$/
I don't know what's wrong with it and why it crashes on safari but it doesn't crash my chrome storybook
Traditional JavaScript doesn't support look-behind assertions ((?<=...)) in regexes. Apparently Chrome has started implementing them, but most other browsers have not.
However, I don't see why you need look-behind at all here:
/^(.*(#))([^# ])*(<br>)?$/
seems like it should achieve the same thing.

Invalid regular expression: Invalid group in node.js

I am trying to map ELB load-balancer logs to have a common format like nginx and have this regex
const splitElbEntry = (elbLogEntry) => R.match(/(?P<date>[0-9-]+T[0-9:]+)\.\S+/)
I get this error:
SyntaxError: Invalid regular expression: /(?P<date>[0-9-]+T[0-9:]+)\.\S+/: Invalid group
where as on https://regexr.com/3o06l it is finding the timestamp, although if I add a new group the regex fails.
What will be the correct javascript equivalent for https://regex101.com/r/JOlrxS/5
Any advice is much appreciated
The selected flavor in regexr is PCRE and your end flavor is JS. JS doesn't support (?P<name>...) notation. However, (?<name>...) would be implemented in ECMAScript 2018 (currently supported by Google Chrome).
You can't, as of now, use a named capturing group that works among all major browsers. Deal with simple numbered capturing groups.
I got the required result using numbered capturing groups, thanks for the help.
https://regex101.com/r/JOlrxS/6
([0-9-]+T[0-9:]+)\.\S+\s+\S+\s+(\S+):\d+\s+\S+:\d+\s+\S+\s+(\S+)\s+\S+\s+(\S+)\s+\S+\s+\S+\s+(\S+)\s+\"\S+\s+\w+:\/\/([\w\-\.]*):\d+(\/\S*)\s+[^\"]+\"\s+\"([^\"]+)\"\s+\S+\s+\S+

help making a "universal" regex Javascript compatible

I found a very nice URL regex matcher on this site: http://daringfireball.net/2010/07/improved_regex_for_matching_urls . It states that it's free to use and that it's cross language compatible (including Javascript). First of all, I have to escape some of the slashes to get it to compile at all. When I do that, it works fine on Rubular.com (where I generally test regexes), with the strange side effect that each match has 5 fields: 1 is the url, and the extra 4 are empty. When I put this in JS, I get the error "Invalid Group". I am using Node.js if that makes any difference, but I wish I could understand that error. I'd like to cut back on the unnecessary empty match fields, but I don't even know where to begin diagnosing this beast. This is what I had after escaping:
(?xi)\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’] ))
Actually, you don't need the first capturing group either; it's the same as the whole match in this case, and that can always be accessed via $&. You can change all the capturing groups to non-capturing by adding ?: after the opening parens:
/\b(?:(?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\((?:[^\s()<>]+|(\(?:[^\s()<>]+\)))*\))+(?:\((?:[^\s()<>]+|(?:\(?:[^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i
That "invalid group" error is due to the inline modifiers (i.e., (?xi)) which, as #kirilloid observed, are not supported in JavaScript. Jon Gruber (the regex's author) was mistaken about that, as he was about JS supporting free-spacing mode.
Just FYI, the reason you had to escape the slashes is because you were using regex-literal notation, the most common form of which uses the forward-slash as the regex delimiter. In other words, it's the language (Ruby or JavaScript) that requires you to escape that particular character, not the regex. Some languages let you choose different regex delimiters, while others don't support regex literals at all.
But these are all language issues, not regex issues; the regex itself appears to work as advertised.
Seemes, that you copied it wrong.
http://www.regular-expressions.info/javascript.html
No mode modifiers to set matching options within the regular expression.
No regular expression comments
I.e. (?xi) at the beginning is useless.
x is useless at all for compacted RegExp
i can be replaced with flag
All these result in:
/\b((?:[a-z][\w-]+:(?:\/{1,3}|[a-z0-9%])|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}\/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))/i
Tested and working in Google Chrome => should work in Node.js

How to find a URL within full text using regular expression

What is wrong with the following regular expression, which works in many online JavaScript regular expression testers (and RegEx Buddy), yet doesn't work in my application?
It is intended to replace URLs with a Hyperlink. The Javascript is found in a javascript file.
var fixed = text.replace(/\b(https?|ftp|file)://[-A-Z0-9+&##/%?=~_|$!:,.;]*[A-Z0-9+&##/%=~_|$]/ig, "<a href='$&' target='blank'>$&</a>");
Chrome, for example, complains that & is not valid (as does IE8). Is there some way to escape the ampersand (or whatever else is wrong), without resorting to the RegEx object?
Those testers let you input the regex in its raw form, but when you use it in source code you have to write it in the form of a string literal or (as is the case here) a regex literal. JavaScript uses forward-slashes for its regex-literal delimiters, so you have to escape any slashes in the regex itself to avoid confusing the interpreter.
Once you escape the slashes it should stop complaining about the ampersand. That was most likely caused by the malformed regex literal.
I recognize that regex, having used it myself the other day; you got it from RegexBuddy's Library, didn't you? If you had used RB's "Use" feature to create a JS-compatible regex, it would have escaped the slashes for you.
This works for me in Chrome
var fixed = text.replace(/(ftp|http|https):\/\/(\w+:{0,1}\w*#)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%#!\-\/]))?/igm, "<a href='$1' target='blank'>$1</a>");

Categories

Resources