Passing regular expression from C# to Javascript not working - javascript

In my code I am passing the following regex from VB.net to javascript for Post Box address validation.
I am passing the following regex from VB.net
^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)
It works good in javascript. But its not case sensitive, it fails when we enter capital letters.
I tried the following regex for case insensitive, but it fails completely.
^ *((#\d+)|((box|bin)[-. \/\\]?\d+)|(.*p[ \.]? ?(o|0)[-. \/\\]? *-?((box|bin)|b|(#|num)?\d+))|(p(ost)? *(o(ff(ice)?)?)? *((box|bin)|b)? *\d+)|(p *-?\/?(o)? *-?box)|post office box|((box|bin)|b) *(number|num|#)? *\d+|(num|number|#) *\d+)/i
My requirement is that I need to pass the RegEx from VB.net to javascript.
Any help?

Passing /i would only work if you were using the literal syntax:
var rex = /^X$/i;
As you are calling the RegExp constructor you need to provide the i option as an argument:
var rex = new RegExp("^X$", "i");

Related

replace in javascript with case conversion

I want to make the first character of every word in a string to uppercase.
i am referring to this article Replacement Text Case Conversion.
when i am running the regular expression ([a-zA-Z])([a-zA-Z](\s)) with the replacement text as \u$1\l$2 in my editor (sublime text) it works fine.
However, when i am trying to do the same in javascript using replace method as below, its giving syntax errors and hence fails.
var regex = /([a-zA-Z])([a-zA-Z]*(\s)*)/gi;
var rep = "\\u$1\\l$2"; // here it is giving error
var result = input.replace(regex,rep);
How to resolve this?
I know this problem can be solved using charAt() and toUppercase() method. but I want to do it using regex with replace. :)
JS regex engine does not support lower- and uppercasing operators \u and \l in the replacement patterns. Use a callback inside String#replace:
var input = "aZ";
var regex = /([a-zA-Z])([a-zA-Z]*(\s)*)/gi;
var result = input.replace(regex, function($0,$1,$2,$3) {
return $1.toUpperCase() + $2.toLowerCase();
});
console.log(result);
Note that you can reduce your pattern to /([a-z])([a-z]*\s*)/gi.

Equivalent regex pattern from c# to javascript

Convertion of the regular expression from C# to javascript.
C#
(?<![\\]);
Javascript
/(?<![\\]);/
While using Regex.split, the regular expression for C# works fine but in javascript 'Unexcpected Quantifier' error occurs.
string
"CN=s\,tttrrr,OU=OU1,DC=dom1,DC=local;CN=g\;hi\,klm,OU=OU1,DC=dom1,DC=local;CN=rrr\ttt,OU=OU1,DC=dom1,DC=local;CN=Vvvv,OU=OU1,DC=dom1,DC=local"
Result
CN=s\,tttrrr,OU=OU1,DC=dom1,DC=local
CN=g\;hi\,klm,OU=OU1,DC=dom1,DC=local
CN=rrr\ttt,OU=OU1,DC=dom1,DC=local
CN=Vvvv,OU=OU1,DC=dom1,DC=local
Splitting the input based on ; which was preceded by a word boundary will give you the desired output.
> var str = "CN=s\\,tttrrr,OU=OU1,DC=dom1,DC=local;CN=g\\;hi\\,klm,OU=OU1,DC=dom1,DC=local;CN=rrr\ttt,OU=OU1,DC=dom1,DC=local;CN=Vvvv,OU=OU1,DC=dom1,DC=local"
undefined
> str.split(/\b;/g)
[ 'CN=s\\,tttrrr,OU=OU1,DC=dom1,DC=local',
'CN=g\\;hi\\,klm,OU=OU1,DC=dom1,DC=local',
'CN=rrr\ttt,OU=OU1,DC=dom1,DC=local',
'CN=Vvvv,OU=OU1,DC=dom1,DC=local' ]
DEMO

Regular expression failed in URL address test

I try to build an Regular expression to check valid URL address. for now I tested different address and all was good , but those next (valid) address's failed:
url = "http://example.com/tr/vvf/index.php/docs/po/trf"
//url = "http://example-a.mydomain.com/test/ny" also not working
var pattern = new RegExp("(https|ftp|http)://[\w-]+(\.[\w-]+)+([\w.,#?^=%&:/~+#-]*[\w#?^=%&/~+#-])?");
pattern.test(url)
I think because of the index.php/doc... Any ideas how to fix it
Just use regex literal instead of RegExp object:
var pattern = /(https|ftp|http):\/\/[\w-]+(\.[\w-]+)+([\w.,#?^=%&:\/~+#-]*[\w#?^=%&\/~+#-])?/;
RegExp works with a string, that requires you to do double escaping so \w becomes \\w in it.
See it working here

How to use inline regex modifier in VB.NET

I'm using jquery validate for client side email validation.
Of course I also have server side validation and want to use the same regex as jquery does to validate the input on the server side.
I found this regex in the source of jquery validate:
// http://docs.jquery.com/Plugins/Validation/Methods/email
email: function( value, element ) {
// contributed by Scott Gonzalez: http://projects.scottsplayground.com/email_address_validation/
return this.optional(element) || /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$/i.test(value);
},
Note the /iat the end of the regex to make the whole thing case insensitive.
I have a complex site with c# libraries Ánd VB libraries.
In both libraries I need to implement this email validation.
In C# I'm using this code:
Regex RegexEmailAddress = new Regex(#"^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$", RegexOptions.Compiled | RegexOptions.Singleline | RegexOptions.IgnoreCase);
Note RegexOptions.IgnoreCase at the end.
However, In my VB code I need a string that hold the regex pattern.
Public Const MAIL_REGEX As String = "^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$"
So far I coudn't find any working regex adjustment to make this regex case insensitive.
I tried adding (?i) in front of the string but it's logged as invalid regex when using it on my website.
also add /i at the end of the pattern gives an invalid regex.
Update:
I tried another method with inline regex modifier I found in this SO question.
Case sensitive: ^[0-9]\s(lbs|kg|kgs)$
Case insensitive: (?i:^[0-9]\s(lbs|kg|kgs)$)
But it's also not working.
Here's the javascript error I get:
Uncaught SyntaxError: Invalid regular expression: /(?i:^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$)/: Invalid group
Update 2
I'm using RegularExpressionValidator
RegularExpressionValidator rxvalEmail = new RegularExpressionValidator();
rxvalEmail.ID = "rxvalEmail";
rxvalEmail.ValidationExpression = SomeHelperInVB.MAIL_REGEX;
rxvalEmail.ControlToValidate = "txtEmail";
So how can I make my regex case insensitive using a inline regex modifier? Of any other sollution to solve this?
I made a simple console application and by using (?i) it works just fine:
Module Module1
Public Const MAIL_REGEX As String = "^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$"
Public Const MAIL_REGEX_I As String = "(?i)^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))#((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))$"
Sub Main()
Dim r_sensitive As New System.Text.RegularExpressions.Regex(MAIL_REGEX)
Dim r_insensitive As New System.Text.RegularExpressions.Regex(MAIL_REGEX_I)
' Returns true
Console.WriteLine("ok#ok.com: " + r_sensitive.IsMatch("ok#ok.com").ToString)
' Returns false
Console.WriteLine("NOT_ok#ok.com: " + r_sensitive.IsMatch("NOT_ok#ok.com").ToString)
' Returns true
Console.WriteLine("ok#ok.com: " + r_insensitive.IsMatch("ok#ok.com").ToString)
' Returns true
Console.WriteLine("NOT_ok#ok.com: " + r_insensitive.IsMatch("NOT_ok#ok.com").ToString)
Console.Read()
End Sub
End Module
Update:
OK, so you are using RegularExpressionValidator to do validation on both client and server. ..
Since the syntax is different between VB-RegEx and Javascript-Regex I'm not sure it's possible to do what you want. In VB you use (?i) in the beginning of the expression whereas in Javascript you need to add /i as a modifier when construction the expression (http://www.w3schools.com/jsref/jsref_obj_regexp.asp).
Maybe you need to use another expression, which doesn't require a "case-insensitive modifier". This one is from Expresso (http://www.ultrapico.com/expresso.htm):
([a-zA-Z0-9_\-\.]+)#((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})
Or, another option would be to replace [a-z] with [a-zA-Z] in your current expression.
Neither of the two expressions works when using Internationalized Domain Names, like "any.name#domännamn.se" (notice the "ä" in the domain name).
Some information that might could help others:
I found out why the regex modifiers aren't working for RegularExpressionValidator when debugging the client side code used for validation.
The code below shows how client validation for regex is done using RegularExpressionValidator control:
function RegularExpressionValidatorEvaluateIsValid(val) {
var value = ValidatorGetValue(val.controltovalidate);
if (ValidatorTrim(value).length == 0)
return true;
var rx = new RegExp(val.validationexpression); // Error: Invalid regular expression:....
var matches = rx.exec(value);
return (matches != null && value == matches[0]);
}
new RegExp("pattern/i") will be formatted as: /pattern/i/
and that's indeed no valid regex. The valid regex would be: /pattern/i
=> Conclusion: Modifiers can't work using RegularExpressionValidator. I'll probably write use CustomValidator control instead with custom server and client side code.
Another option would be to replace [a-z] with [a-zA-Z] in the used regex

RegEx test for string ending?

I'm horrible at RegEx and I need a regex to test if a certain string ends a certain way. For example, if the RegEx tests for ending with foo, "somestringfoo" -> True and "someotherFoostring" -> False. It needs to be case sensitive and work with alphanumeric and underscore. Here is what I've got, but I can't get it to work:
var test = RegExp.test('/foo$/');
You would do it this way:
/foo$/.test("somestringfoo")
test is a method of the regexp object, so it would be /foo$/.test(someString) or new Regexp("foo$").test(someString).
However, testing a string for ending with a certain substring does not need regular expressions, see endsWith in JavaScript.
this should do the work:
function isFoo(string){
var pattern = /foo$/;
return pattern.test(string);
}

Categories

Resources