Regex expression for accepsts only ()+ and numbers - javascript

I need a regular expression to use with Javascript .test
like using this examples
+999904321493214032
and
(+999)432143214
and
432143124321
Can some one please help me with this?

The tightest I can think of is:
^(?:\(\+\d+\)|\+)?\d+$
See live demo showing matching your examples and not matching input with the correct characters but that are malformed (eg imbalanced brackets).

Related

Regex for - 'A,B','C'

I have written this regex -
([\s]*'[A-Za-z0-9_: ]*[\,]*[\s]*[A-Za-z0-9_: ]*\'[\s]*)[\,]*
But this is not handling the input - 'A,B' 'C' - In this the comma is missing, still its a perfect match.
Can anyone please help.
After giving this more thought, I think what you want is something more like this:
^(?<item>\'[a-zA-Z0-9,\s]+\')(\s*,(?&item))*\s*$
You're using an asterisk which will match zero instances. Try using + instead for the characters you want one or more of.
Please provide other examples that you'd expect to match. For this specific case, the following would match, but is very rigid and specific:
\'+[a-zA-Z]+\,\s*[a-zA-Z]+\'+\,\s*\'+[a-zA-Z]+\'+
Edit:
This is more in line with what I think you want:
^(\'[a-zA-Z]+(\,+\s*[a-zA-Z]+)*\'\s*\,*)*$

Regular expression validation in Javascript

I am looking to validate an input text against the following pattern in JS/JQuery: <some_string>:<some_string>.
Examples:
A110:B120
AB12C:B123
I know this might be too naive, but appreciate any help here.
You could use this:
^[A-Z0-9]+:[A-Z0-9]+$
That will match your examples and any other that has at least 1 character in each side and only has upper case letters and numbers.
You can refer to this answer in order to know how to test a regex against a string.
Try this
"A110:B120 AB12C:B123".match(/(\w+:\w+)/);
MATCH
1. `A110:B120`
2. `AB12C:B123`
or
"A110:B120".match(/(\w+)+:+(\w+)/);
MATCH
1. A110
2. B120

Incorrect Regex Expression

I found this site:
https://mathiasbynens.be/demo/url-regex
and wanted to use for my url validation the regex from the #diegoperini, because according to the table provided on the top of the site, it is the best regex.
When I try to use it, I get a range value error.
P.S. I am using the following Regex expression:
_^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)(?:\.(?:[a-z\x{00a1}-\x{ffff}0-9]+-?)*[a-z\x{00a1}-\x{ffff}0-9]+)*(?:\.(?:[a-z\x{00a1}-\x{ffff}]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$_iuS
and the following online validator:
http://regexr.com/
It does show the error place in the regex, but I don't know how to manage it. I tried to swap the both ranges, but it doesn't do the trick.
I would appreciate some help.
P. P. S.
I use the regex in the AngularJS directive to validate url input.
Buried within your character classes, you have this range:
\x{00a1}-\x{ffff}
But it should be:
\u00a1-\uffff
Your expression \x{00a1}-\x{ffff} is not the correct syntax for a hex encoding or a character and as-is means any of "x{}0a1f" plus the range "}-x", but "x" is less than "}" so an error is raised to that effect.
This should work
^(?:(?:https?|ftp):\/\/)(?:\S+(?::\S*)?#)?(?:(?!10(?:\.\d{1,3}){3})(?!127(?:\.\d{1,3}){3})(?!169\.254(?:\.\d{1,3}){2})(?!192\.168(?:\.\d{1,3}){2})(?!172\.(?:1[6-9]|2\d|3[0-1])(?:\.\d{1,3}){2})(?:[1-9]\d?|1\d\d|2[01]\d|22[0-3])(?:\.(?:1?\d{1,2}|2[0-4]\d|25[0-5])){2}(?:\.(?:[1-9]\d?|1\d\d|2[0-4]\d|25[0-4]))|(?:(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)(?:\.(?:[a-z\u00a1-\uffff0-9]+-?)*[a-z\u00a1-\uffff0-9]+)*(?:\.(?:[a-z\u00a1-\uffff]{2,})))(?::\d{2,5})?(?:\/[^\s]*)?$

Need help in regex pattern

i have this Regex pattern
\=[a-zA-Z\.\:\[\]_\(\)\&\$\%#\-\#\!0-9;=\?/\+\xBF\~]+[?\s+|?>]
and i have this HTML
1.esc#xyz.com
2.johnross#zys.com
3.johnross#wen.com
Here the problem is,
I need to avoid first and second as it has white space as well and it is valid attributes.
But only the third one is working as it does't has white spaces.
means nothing should be selected with the above pattern.
here is direct link to test
http://regexr.com?31r61
Please help!
Thanks,
EDIT:
If you just want to match unquoted attributes, this should work:
[<\s]+[\w]+(=[^\"][^\s>]*)
Kind of inelegant but let me know if that does what you want.
Which pattern are you trying to match? All three? And if so, which portion? The subject or the email? If you're just trying to match the subject, try using this as the pattern to match:
\=\"mailto:[^?]*\?subject=([^\"]*)\"\>
That will return a match where the group is the subject itself.
That is a wicked character class....
why don't you try something a bit more reasonable. Try this...
\=".*?(?<!\\)"
that will match anything in the parenthesis after href if that's what you're trying to get. If you're looking for more than that, this regex can easily by modified.

Javascript Regular Expressions Lookbehind Failing

I am hoping that this will have a pretty quick and simple answer. I am using regular-expressions.info to help me get the right regular expression to turn URL-encoded, ISO-8859-1 pound sign ("%A3"), into a URL-encoded UTF-8 pound sign ("%C2%A3").
In other words I just want to swap %A3 with %C2%A3, when the %A3 is not already prefixed with %C2.
So I would have thought the following would work:
Regular Expression: (?!(\%C2))\%A3
Replace With: %C2%A3
But it doesn't and I can't figure out why!
I assume my syntax is just slightly wrong, but I can't figure it out! Any ideas?
FYI - I know that the following will work (and have used this as a workaround in the meantime), but really want to understand why the former doesn't work.
Regular Expression: ([^\%C2])\%A3
Replace With: $1%C2%A3
TIA!
Why not just replace ((%C2)?%A3) with %C2%A3, making the prefix an optional part of the match? It means that you're "replacing" text with itself even when it's already right, but I don't foresee a performance issue.
Unfortunately, the (?!) syntax is negative lookahead. To the best of my knowledge, JavaScript does not support negative lookbehind.
What you could do is go forward with the replacement anyway, and end up with %C2%C2%A3 strings, but these could easily be converted in a second pass to the desired %C2%A3.
You could replace
(^.?.?|(?!%C2)...)%A3
with
$1%C2%A3
I would suggest you use the functional form of Javascript String.replace (see the section "Specifying a function as a parameter"). This lets you put arbitrary logic, including state if necessary, into a regexp-matching session. For your case, I'd use a simpler regexp that matches a superset of what you want, then in the function call you can test whether it meets your exact criteria, and if it doesn't then just return the matched string as is.
The only problem with this approach is that if you have overlapping potential matches, you have the possibility of missing the second match, since there's no way to return a value to tell the replace() method that it isn't really a match after all.

Categories