How to write a regex which matches anything but a number - javascript

I am trying to create an angular directive to be used with HTML inputs to filter out none numeric characters
Here is my regex I am using to achieve that:
inputValue.replace(/[^0-9\-\.]/g, "").replace(/\.(\.)/g, '$1');
However this regex does not cover these cases:
--5
5.5.6
-5-5

If I'm not wrong, this is really simple.^^
\d
\d machtes every digit from [0-9]. You can test our RegEx very simple at https://regex101.com without writing any javascript code to test it.
Edit:
You might want to add a * to the \d.
\d*
The * is the greedy selector, which selects all of the type before.

In your regex you use a negated character class [^0-9\-\.] which matches not a digit 0-9, a - and a . so you are keeping those matches.
If you want to match anything but a number you could use [^0-9] or \D to match any character that is not a digit and replace that with an empty value.
let inputValue = `--5
5.5.6
-5-5
!##$%# $%#% $%435 452545`;
inputValue = inputValue.replace(/\D/g, "");
console.log(inputValue);

Related

Further narrow down regex character set so that one uppercase is mandatory

I have a long string like aaaaa123 ** bbbbb444 ** ccccc66 ** ddDddD77 ** eeeee667 and want to grab a substring using a Regex.
What I've got so far:
/[A-Z,a-z,0-9,_-]{7,14}/
This should return any group of 7 to 14 characters consisting of uppercase, lowercase, digits - and _
So far so good.
However, I now want it to be more strict so that at least one uppercase character is mandatory. I.e. in my example only ddDddD77 should match. How do I do that?
If a lookahead and \w is supported and the word does not start or end with - you can assert for the length and match at least an uppercase:
\b(?=[\w-]{7,14}\b)[a-z0-9_-]*[A-Z][\w-]*
Regex demo
Another option:
(?<!\S)(?=[\w-]{7,14}(?!\S))[a-z0-9_-]*[A-Z][\w-]*
Regex demo
If the lookbehind is not supported, you could also opt for a capture group:
(?:\s|^)(?=[\w-]{7,14}(?:\s|$))([a-z0-9_-]*[A-Z][\w-]*)
Regex demo

RegEx for single asterisk (*) and alphanumeric

I need a javascript regex that accepts any alphanumeric character (can be any amount of characters or 0 characters if an asterisk is present) and a single asterisk anywhere in the string (but it does not need the asterisk).
Matches
*
abc
*abc
abc*
a*bc
Invalid Matches
**
*_abc
*abc*
abc**
**abc
I have
^([A-Za-z\d*]?)+$
but that matches multiple asterisks and I'm not sure how to only allow one https://regex101.com/r/a1C9bf/1
You may use this regex with a negative lookahead:
/^(?!(?:.*\*){2})[A-Za-z\d*]+$/gm
Updated RegEx Demo
Negative lookahead (?!(?:.*\*){2}) fails the match if there are more than one * in input.
Without requiring any look-ahead, you could use ^([\da-zA-Z]+|[\da-zA-Z]*\*[\da-zA-Z]*)$
https://regex101.com/r/xW2IvR/2
You could do:
^(?=.)[A-Za-z\d]*\*?[A-Za-z\d]*$
This will match any string that that's at least one character long ((?=.)), starts with zero or more alphanumeric characters, contains an optional *, and ends with zero or more alphanumeric characters.
You could also replace [A-Za-z\d] with [^\W_] to make it a little shorter (but slightly harder to read):
^(?=.)[^\W_]*\*?[^\W_]*$
You want one match one of two possible cases:
an asterisk surrounded by zero or more alphanumeric characters
one or more alphanumeric characters
Then this is your regex:
^([a-zA-Z\d]*\*[a-zA-Z\d]*|[a-zA-Z\d]+)$

Matching any character followed by not numbers

I am not good at RegEx. Though it seems very simple to achieve but I am not able to find out the way to match any characters followed by not numbers. I am trying with negative lookahead. If I use any word it is working as expected but if I try to match any character with square bracket, it is failing.
var pattern = /sample(?!\d)/;
console.log(pattern.test("sample324")); //false
var pattern = /[a-z]+(?!\d)/;
console.log(pattern.test("sample324")); //true but expect false
Thanks in advance.
Problem is that [a-z]+(?!\d) will let it match any 1+ characters not followed by a digit, so it will match sampl in your input satisfying assertion of non-digit at next position.
You may use this regex with a negative lookahead:
/^(?!.+\d)/
This will fail the match if a digit appears anywhere in input after 1+ of any character.
RegEx Demo
For better efficiency, you may use this regex as well:
/^(?!\D+\d)/
Which fails if there 1+ non-digits followed by a digit anywhere in input.
I think this may work:
var pattern = /[^0-9]/.test('mystring9')

regex replace certain character but not for particular set in javascript

var str='select * from where item1=abcd and price>=20';
I am using the below code to replace the '=' to empty space
str=str.replace(/[=]/g, " ")
but it is also replacing '>=' . I want >= not to be replaced with any thing and also for some others condition like '==' or '<=' etc.
So my output should be - 'select * from where item abcd and price>=20'
Please help me to achieve this.
Use below regex for replacement
/([a-z0-9]+)\s*=\s*([a-z0-9]+)/gi
and replace it with $1 $2.
([a-z0-9]+): Match one or more alphanumeric characters and add them to capturing group
\s*: Zero or more space characters
=: Equal sign
gi: g: Global flag to match all possible matches. i: Case-insensitive flag.
$n in the replacement part is the nth captured group value.
var regex = /([a-z0-9]+)\s*=\s*([a-z0-9]+)/gi;
var str = 'select * from where item1=abcd and price>=20';
console.log(str.replace(regex, '$1 $2'));
Replace an equal sign with a letter or number on either side with the corresponding characters around a space.
str.replace(/([a-zA-Z0-9])=([a-zA-Z0-9])/, '$1 $2')
In regex [] means "the set of", so [a-zA-Z0-9] is one character from the set of any lowercase, uppercase, or digit.
Simple and dirty trick. Remove g from regx
var str='select * from where item1=abcd and price>=20';
console.log(str.replace(/[=]/, " "))
A good way to approach these problems is to capture everything you wish to skip, and then not capture everything you wish you remove. In your case:
(>=|<=|==|'[^']*(?:''[^']*)*')|=
and replace with $1.
Working example: https://regex101.com/r/3pT9ib/3
First we have a capturing group: (...), which is captured into $1.
The group matched >= and <=. I also threw in == (is this valid in SQL?) and escaped SQL strings, just for the example.
If we were not able to match the group, we can safely match and remove the leftover =.
This approach is explained nicely here: Regex Pattern to Match, Excluding when... / Except between

JS Regex lookahead

I want to be able to match these types of strings (comma separated, and no beginning or trailing spaces):
LaunchTool[0].Label,LaunchTool[0].URI,LaunchTool[1].Label,LaunchTool[1].URI,LaunchItg[0].Label,LaunchItg[0].URI,csr_description
The rules, in English, are:
1) Zero or more instances of [] where the brackets must contain only one number 0-9
2) Zero or more instances of ., where . must be followed by a letter
3) Zero or more instances of _, where _ must be followed by a letter
I currently have this regex:
/^([a-z]){1,}(\[[0-9]\]){0,}(\.){0,}[a-z]{1,}$/i
I cannot figure out why
"aaaa" doesn't match
furthemore,
"aaaa[0].a" matches, but "aaaa[0]" does not...
anyone know what's wrong? I believe I might need a lookahead to make sure . and _ characters are followed by a letter? Perhaps I can avoid it.
this regex can match "aaaa", try getting value of
(/^([a-z]){1,}(\[[0-9]\]){0,}(\.){0,}[a-z]{1,}$/i).test("aaaa")
"aaaa[0]" does not match, because there is [a-z]{1,} in the end of expression. once "[0]" is matched by (\[[0-9]\]){0,}, trailing [a-z]{1,} must be shown at the end of string
Use optional capture groups. Example: ([a-z])?.
Here is what i ended up with:
/^((\w+)(\[\d+\])?\.?(\w+)?,?)+$/
Shorthands explanation:
* = {0,}
+ = {1,}
\w = [A-Za-z0-9_]
\d = [0-9]

Categories

Resources