I'm using reactive form validation under my Angular app .
i Have a form validation condition which demands that my string didn't end with
- or '
i ve tried this , but it seems to not work :
Validators.pattern('.*(?<!\-\')$')
Suggestions ?
The (?<!\-\') pattern is a negative lookbehind that matches a location that is not immediately preceded with a -' substring, while you only want to fail the match if there is either - or a ' at the end.
You may use the following solution that uses a regex literal notation, so that you didn't have to write a pattern to match the whole input:
Validators.pattern(/[^-']$/)
Or, if the string can be empty,
Validators.pattern(/(?:^|[^-'])$/)
The (?:^|[^-'])$ pattern matches either start of a string (^) or any char but a - or ' (with the help of the negated character class, [^-']), and $ asserts the end of the string.
Related
At the momemnt I'm using this regular expression to validate address
(!/^[A-Za-z]/i.test(street))
When I use address like this - Esterwergen - it works.
But when I added the sign before the title - 'Esterwergen - it shows my error.
Ho I can modify my RegEx and allow to use this sign before the name?
To allow an optional leading ', you'd change your regexp from
/^[A-Za-z]/
to
/^'?[A-Za-z]/
where the ? means "zero or one times".
If you want to allow the ' anywhere in your string,
/^['A-Za-z]/
would do the trick.
In addition, be sure that you realize that you're only checking the first character of the string as it is.
Right now you will allow Ester9ui4y6ewigkdlLNDSKJ#€=# :::.
To constrain that, you'll need the + quantifier and the $ (end-of-string) anchor.
/^[A-Za-z]+$/
Lets see what is your RegExp targeting:
/^[A-Za-z]/i
^: Asserts position at start of the string.
[]: Match a single character depending on what's inside.
A-Z: Match uppercase letters from A to Z.
a-z: Match lowercase letters from a to z.
i: Case-insensitive.
Consider this:
Using [A-Za-z] along with i flag is redundant. Use /^[a-z]/i or /^[A-z]/ instead.
Using [a-zA-Z\u00C0-\u00FF] for example extends matching to latin characters using UNICODE syntax. See full UNICODE reference here.
Use /^['a-z]+/i to allow ' anywhere in the string.
Use /^'?[a-z]+/i to allow ' only at the beginning of the string. ? means '1 time or 0 times'.
To play around with RegExp you can use tools like this.
I have a standard expression that is not working correctly.
This expression is supposed to catch if a string has invalid characters anywhere in the string. It works perfect on RegExr.com but not in my tests.
The exp is: /[a-zA-Z0-9'.\-]/g
It is failing on : ####
but passing with : aa####
It should fail both times, what am I doing wrong?
Also, /^[a-zA-Z0-9'.\-]$/g matches nothing...
//All Boxs
$('input[type="text"]').each(function () {
var text = $(this).prop("value")
var textTest = /[a-zA-Z0-9'.\-]/g.test(text)
if (!textTest && text != "") {
allFieldsValid = false
$(this).css("background-color", "rgba(224, 0, 0, 0.29)")
alert("Invalid characters found in " + text + " \n\n Valid characters are:\n A-Z a-z 0-9 ' . -")
}
else {
$(this).css("background-color", "#FFFFFF")
$(this).prop("value", text)
}
});
edit:added code
UPDATE AFTER QUESTION RE-TAGGING
You need to use
var textTest = /^[a-zA-Z0-9'.-]+$/.test(text)
^^
Note the absence of /g modifier and the + quantifier. There are known issues when you use /g global modifier within a regex used in RegExp#test() function.
You may shorten it a bit with the help of the /i case insensitive modifier:
var textTest = /^[A-Z0-9'.-]+$/i.test(text)
Also, as I mention below, you do not have to escape the - at the end of the character class [...], but it is advisable to keep escaped if the pattern will be modified later by less regex-savvy developers.
ORIGINAL C#-RELATED DETAILS
Ok, say, you are using Regex.IsMatch(str, #"[a-zA-Z0-9'.-]"). The Regex.IsMatch searches for partial matches inside a string. So, if the input string contains an ASCII letter, digit, ', . or -, this will pass. Thus, it is logical that aa#### passes this test, and #### does not.
If you use the second one as Regex.IsMatch(str, #"^[a-zA-Z0-9'.-]$"), only 1 character strings (with an optional newline at the end) would get matched as ^ matches at the start of the string, [a-zA-Z0-9'.-] matches 1 character from the specified ranges/sets, and $ matches the end of the string (or right before the final newline).
So, you need a quantifier (+ to match 1 or more, or * to match zero or more occurrences) and the anchors \A and \z:
Regex.IsMatch(str, #"\A[a-zA-Z0-9'.-]+\z")
^^ ^^^
\A matches the start of string (always) and \z matches the very end of the string in .NET. The [a-zA-Z0-9'.-]+ will match 1+ characters that are either ASCII letters, digits, ', . or -.
Note that - at the end of the character class does not have to be escaped (but you may keep the \- if some other developers will have to modify the pattern later).
And please be careful where you test your regexps. Regexr only supports JavaScript regex syntax. To test .NET regexps, use RegexStorm.net or RegexHero.
/^[a-zA-Z0-9'.-]+$/g
In the second case your (/[a-zA-Z0-9'.-]/g) was working because it matched on the first letter, so to make it correct you need to match the whole string (use ^ and $) and also allow more letters by adding a + or * (if you allow empty string).
Try this regex it matches any char which isn't part of the allowed charset
/[^a-zA-Z0-9'.\-]+/g
Test
>>regex = /[^a-zA-Z0-9'.\-]+/g
/[^a-zA-Z0-9'.\-]+/g
>>regex.test( "####dsfdfjsakldfj")
true
>>regex.test( "dsfdfjsakldfj")
false
I have this regular expression:
/([a-záäéěíýóôöúüůĺľŕřčšťžňď])-$\s*/gmi
This regex selects č- from my text:
sme! a Želiezovce 2015: Spoloíč-
ne pre Európu. Oslávili aj 940.
But I want to select only - (without č) (if some character from the list [a-záäéěíýóôöúüůĺľŕřčšťžňď] is before the -).
In other languages you would use a lookbehind
/(?<=[a-záäéěíýóôöúüůĺľŕřčšťžňď])-$\s*/gmi
This matches -$\s* only if it's preceded by one of the characters in the list.
However, Javascript doesn't have lookbehind, so the workaround is to use a capturing group for the part of the regular expression after it.
var match = /[a-záäéěíýóôöúüůĺľŕřčšťžňď](-$\s*)/gmi.match(string);
When you use this, match[1] will contain the part of the string beginning with the hyphen.
First, in regex everything you put in parenthesis will be broken down in the matching process, so that the matches array will contain the full matching string at it's 0 position, followed by all of the regex's parenthesis from left to right.
/[a-záäéěíýóôöúüůĺľŕřčšťžňď](-)$\s*/gmi
Would have returned the following matches for you string: ["č-", "-"] so you can extract the specific data you need from your match.
Also, the $ character indicates in regex the end of the line and you are using the multiline flag, so technically this part \s* is just being ignored as nothing can appear in a line after the end of it.
The correct regex should be /[a-záäéěíýóôöúüůĺľŕřčšťžňď](-)$/gmi
This seems a very simple question but I haven't been able to get this to work.
How do I convert the following string:
var origin_str = "abc/!/!"; // Original string
var modified_str = "abc!!"; // replaced string
I tried this:
console.log(origin_str.replace(/\\/,''));
This only removes the first occurrence of backslash. I want to replaceAll. I followed this instruction in SO: How to replace all occurrences of a string in JavaScript?
origin_str.replace(new RegExp('\\', 'g'), '');
This code throws me an error SyntaxError: Invalid regular expression: /\/: \ at end of pattern. What's the regex for removing backslash in javascript.
A quick basic overview of regular expressions in JavaScript
When using regular expressions you can define the expression on two ways.
Either directly in the function or variable by using /regular expression/
Or by using the regExp contructor: new RegExp('regular expression').
Please note the difference between the two ways of defining. In the first the search pattern is encapsuled by forward slashes, while in the second one the search pattern is passed as a string.
Remember that regular expressions is in fact a search language with it's own syntax. Some characters are used to define actions: /, \, ^, $, . (dot), |, ?, *, +, (, ), [, {, ', ". These characters are called metacharacters and need to be escaped if you want them to be part of the search pattern. If not they will be treated as an option or generate script errors. Escaping is done by using the backslash. E.g. \\ escapes the second backslash and the search pattern will now search for backslashes.
There are a multitude of options you can add to your search pattern.:
Examples
adding \d will make the pattern search for a numeric value between [0-9] and/or the underscore. Simple regular expressions are parsed from left to right.
/javascript/
Searches for the word javascript in a string.
/[a-z]/
When a pattern is put between square bracket the search pattern searches for a character matching any one of the values inside the square brackets. This will find d in 229302d34330
You can build a regular expression with multiple blocks.
/(java)|(emca)script/
Find javascript or emcascript in a string. The | is the or operator.
/a/ vs. /a+/
The first matches the first a in aaabbb, the second matches a repetition of a until another character is found. So the second matches: aaa.
The plus sign + means find a one or more times. You can also use * which means zero or more times.
/^\d+$/
We've seen the \d earlier and also the plus sign. This means find one or more numeric characters. The ^ (caret) and $ (dollar sign) are new. The ^ says start searching from the begin of the string, while the $ says until the end of the string. This expression will match: 574545485 but not d43849343, 549854fff or 4348d8788.
Flags
Flags are operators and are declared after the regular expression /regular expression/flags
JavaScript has three flags you can use:
g (global) Searches multiples times for the pattern.
i (ignore case) Ignores case in pattern.
m (multiline) treat beginning and end characters (^ and $) as working over multiple lines (i.e., match the beginning or end of each line (delimited by \n or \r), not only the very beginning or end of the whole input string)
So a regular expression like this:
/d[0-9]+/ig
matches D094938 and D344783 in 98498D094938A37834D344783.
The i makes the search case-insensitive. Matching a D because of the d in the pattern. If D is followed by one or more numbers then the pattern is matched. The g flag commands the expression to look for the pattern globally or simply said: multiple times.
In your case #Qwerty provided the correct regex:
origin_str.replace(/\//g, "")
Where the search pattern is a single forward slash /. Escaped by the backslash to prevent script errors. The g flags commands the replace function to search for all occurrences of the forward slash in the string and replace them with an empty string "".
For a comprehensive tutorial and reference : http://www.regular-expressions.info/tutorial.html
Looking for this?
origin_str.replace(/\//g, "")
The syntax for replace is
.replace(/pattern/flags, replacement)
So in my case the pattern is \/ - an escaped slash
and g is global flag.
i need a regex pattern for name field in which i want to allow character a-z, A-Z and ' - and white space. i am currently using this code but it is giving an error.
final String regexpattern = "/[a-zA-Z\s-']*/";
You have to add another \ before \s, and add ^ and $ for match begin and end.
final String regexpattern = "/^[a-zA-Z\\s'-]*$/";
Your regex (when it is no longer malformed - see below how to fix that) will always match because you do not use anchors (^ - start of string, and $ - end of string) and use a * quantifier (match 1 or more symbols matching the preceding subpattern, as many as possible).
However, just adding ^ and $ will not fix the pattern, because you are using 1 backslash with \s inside a C string (i.e. inside a string literal "..."). The backslash is treated as an escape symbol and is not taken into account as "\s" is an invalid escape sequence. Inside a character class in regex (i.e. in [...]), a hyphen creates a range. Your s-' creates a range from s (dec. code 115) to ' (dec. code 39). Since in a range inside a character class the codes must go from the lowest to the highest, an error is thrown.
You may just use
final String regexpattern = "/^[a-zA-Z\\s-']*$/";
It is possible because the hyphen after a shorthand class \s does not create a range and is considered a literal. As best practice, you can move it to the end of the character class as xdazz did.
As for your additional question in comment:
i have to allow the all characters excluding ^<>%*()#!?
Just use a negated character class: a pair of square brackets with a starting ^ inside it:
final String regexpattern = "/^[^<>%*()#!^?]*$/";
1 2 3
Here, the first ^ is the start-of-string anchor, and the second caret is the negation of the characters inside the character class. The third caret is a literal symbol ^.