I'm trying to match unicode regular expression but somehow the \p{L} wont work.
<script>
var input="teëst";
var re = /^[a-zA-Z-. \pL]{2,32}$/;
var is_valid=input.match(re);
if(is_valid){
document.write('Regularexpression valid');
} else {
document.write('Regularexpression invalid');
}
</script>
Plnkr.co:
https://plnkr.co/edit/3PCMxqCnwsyrueYQbB8q?p=preview
What am I doing wrong?
UPDATE
https://stackoverflow.com/a/280762/989121
Workaround:
var re = /^[a-zA-Z- \u00c0-\u017e]{2,32}$/;
My google search on javascript online regular expression check brought me to regex101.com and this validated my regexp so during the creation of this question I thought I was doing something wrong elsewhere in the code. Points out unicode is not supported yet.
https://stackoverflow.com/a/280762/989121
Workaround:
var re = /^[a-zA-Z- \u00c0-\u017e]{2,32}$/;
You can use:
var re = /^[a-zA-Z\u00C0-\u017F-. \pL]{2,32}$/;
It uses unicode matching, See here
Try with this regular expression:
var re = /[^\x00-\x7F]+/;
Related
var uriRegExp = /^(sip):\(?([0-9]{3})\)?[- ]?([0-9]{3})[- ]?([0-9]{4})#\w+(\ w+)*(\.\w)(\.\w{2,3})+$/;
Is this a correct regular expression for validating the string
sip:1-999-123-4567#voip-provider.example.net ?
No, this regex does not match your string.
If you want to know why you could have a look at https://regex101.com/r/EC0xFN/1 .
There you can interactively build and check your regex with different input strings.
Here is a simple sip URI validator, that i have created, using regular expression.
function myFunction() {
var str = "sip:+91989556926#test.est.test.com";
var regExp = /^(sip):(\S+[0-9])#\S+(\w+([.-]?\w+)*).(\w{2,3})$/;
var result = regExp.test(str);
document.getElementById("demo").innerHTML = result;
}
Please check the link.
[https://regex101.com/r/5vMfI9/4][1]
I have written a regular expression for validating email and the regular expression is like this. But it is giving me the error of invalid expression term in the regex string after the '#' part.Below is the regex string.
/^([\w-\.]+#([\w-]+\.)+[\w-]{2,4})?$/;
Thanks
Your regex is working fine.
You don't need to escape char other than [ or ] inside a set. Use [\w-.], not [\w-\.] (just an optimization).
Additional note
If you're using var re = new RegExp("...") instead of var re = /.../ you need to escape backslashes or it won't works :
// Works
var re = /^([\w-.]+#([\w-]+\.)+[\w-]{2,4})?$/;
console.log(re.test('marty#bbtf.com'));
// Also works
re = new RegExp("^([\\w-.]+#([\\w-]+\\.)+[\\w-]{2,4})?$");
console.log(re.test('marty#bbtf.com'));
// Does NOT work
re = new RegExp("^([\w-.]+#([\w-]+\.)+[\w-]{2,4})?$");
console.log(re.test('marty#bbtf.com'));
The regex works fine. You need to escape special symbols. Like [ to \[ You could use a function for that:
function escapeRegExp(str) {
return str.replace(/[\-\[\]\/\{\}\(\)\*\+\?\.\\\^\$\|]/g, "\\$&");
}
'^[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]{1}\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}[ -]*\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}\d{1}$'
the above regular expression accepts inputs like T3K2H3 or T3K-2H3 from .net form but when i run the validation through the javascript; it does not work.
var rxPostalCode = new RegExp('^[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]{1}\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}[ -]*\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}\d{1}$');
var postalCode = 't3k2h3';
var matchesPostalCode = rxPostalCode.exec(postalCode);
if (matchesPostalCode == null || postalCode != matchesPostalCode[0]) {
$scope.AccountInfoForm.PostalCode.$setValidity("pattern", false);
$scope.showLoading = false;
return false;
}
I believe that in javascript, you have to do // instead of ''
as follows:
/^[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]{1}\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}[ -]*\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}\d{1}$/
You might want to check the following link:
Validate email address in JavaScript?
You have two syntaxes to define a regexp object:
var rxPostalCode = /^[abceghj-np-tvxy]\d[abceghj-np-tv-z][ -]?\d[abceghj-np-tv-z]\d$/i;
or
var rxPostalCode = new RegExp('^[abceghj-np-tvxy]\\d[abceghj-np-tv-z][ -]?\\d[abceghj-np-tv-z]\\d$', 'i');
Note that with the second syntax you need to use double backslashes.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
"Do not forget to escape \ itself while using the RegExp("pattern") notation because \ is also an escape character in strings."
var rxPostalCode = new RegExp('^[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]{1}\\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}[ -]*\\d{1}[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]{1}\\d{1}$');
That should work, I tested it in Chrome's console.
Try the following pattern:
^[AaBbCcEeGgHhJjKkLlMmNnPpRrSsTtVvXxYy]\d
[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz][ -]*\d
[AaBbCcEeFfGgHhJjKkLlMmNnPpRrSsTtVvWwXxYyZz]\d
Remove the $ at the end and see if that solves your problem.
I also simplified things a bit, the \d{1} is the same as \d
I would also change the [ -]* to [ -]? unless you want to allow multiple spaces or dashes
I suspect what is happening is that the $ expect the end of the line or string, and JavaScript may not store the VAR properly. See if remove the $ solves it, or possibly keeping the $ and trim() the string.
I used preg_match for my server-side validation but I want to have a client side too.
For my PHP I allow those characters:
'/^[A-Za-z][a-zA-Z0-9 .:-,!?]+$/'
How would I make a white list of characters with match() in JavaScript?
EDIT:
I tried this but it didn't work for some reason:
My debugger says, right before the if statement:
218SyntaxError: Invalid regular expression: range out of order in character class
$('#title').blur(function(){
input = $('#title').val();
var invalidChars = /^[^a-z][^a-z\d .:-,!?]+$/i;
if (!invalidChars.test(input)){
alert('true');
}
else {
alert('false');
}
});
all of the above answers are correct, though just a side-note: instead of writing [A-Za-z], a simple /[a-z]/i will suffice. The i is a case-insensitive flag...
var validChars = /^[a-z][a-z\d .:\-,!?]+$/i;
if (validChars.test(myText)){ ... }
Using regex.test(str) is slightly more performant than str.match(regex) if all you want is to know if a match exists or not.
Alternatively, you can early out if you see any invalid character:
var invalidChars = /^[^a-z][^a-z\d .:\-,!?]+$/i;
if (!invalidChars.test(myStr)){
// we passed
}
This allows the regex test to stop the moment it sees a disallowed character.
Try the following
var text = ...;
if (text.match(/^[A-Za-z][a-zA-Z0-9 .:-,!?]+$/)) {
...
}
Actually it's the opposite:
var regexp = /^[A-Za-z][a-zA-Z0-9 .:-,!?]+$/;
if (regexp.test(text)) {
}
I have some links in which people did not add the protocol to. I.e., www.stackoverflow.com. If the link begins with www., I want to replace it with 'http://www.'.
How can I do this with JavaScript regular expressions?
I tried the code below, but I can't seem to match the pattern 'doesn't start with [A-z]+://www.'.
The links are mixed in with text.
jQuery(document).ready(function () {
jQuery('.myClass').each(function (index) {
var temp = wwwify(jQuery(this).text());
jQuery(this).html(temp);
});
});
function wwwify(text) {
var regex = /(?!\b([A-z]+:\/\/))www\./igm;
return text.replace(regex, 'http://www.');
}
Why not just use the following?
if (text.substring(0,4)=='www.') {
text = 'http://'+text;
}
You could just easily replace each "http://www." to "www." and then replace all "www." to "http://www.". It might not be the prettiest regexp you could imagine, but it will solve your problem.
$(document).ready(function () {
$('.myClass').each(function (index) {
var $elm = $(this); // cache $(this) for reuse
var html = $elm.html();
html = html.replace(/http\:\/\/www\./ig, "www.").replace(/www\./ig, "http://www."); ;
$elm.html(html);
});
});
You need to anchor your regex to the start of the string. Also the range needs to be /[a-z]/ as the /i modifier will cover the upper-case possibilities. The /m and /g modifiers are irrelevant here. Leaving
var regex = /^(?![a-z]+:\/\/)www\./i;
My apologies, I missed the part saying "The links are mixed in with text". Without look-behind this can only be done using a function to return a replacement string. I suggest this, which captures any protocol before the www. and replaces it with http:// if it is blank
var regex = /\b([a-z]+:\/\/)?www\./ig;
text.replace(regex, function(url, protocol) {
return protocol ? url : "http://" + url;
});
Since I haven't found any suitable regex solutions through SO or elsewhere, just using regular javascript replace may be the best solution.
For now I'm making two passes through the text:
function wwwLineBeginsWith(text) {
var regex = /^www./gi;
return text.replace(regex, 'http://');
}
function wwwWordBeginsWith(text) {
var regex = /\swww./gi; return text.replace(regex, 'http://');
}
var test1 = 'www.test2.com';
test1 = wwwLineBeginsWith(test1);
test1 = wwwWordBeginsWith(test1);
console.log(wwwWordBeginsWith(test1));
How about replacing those with a protocol regardless?
function wwwify(text) {
return text.replace(/(http(s?):\/\/)?www\./ig, 'http$2://www.');
}
The reason it's currently not working is because JavaScript doesn't support lookbehinds, only lookaheads. You would need the syntax (?<!, which is not available in JavaScript regular expressions.
If you absolutely must use RegExp to determine this, I would recommend using something like /^[^Hh][^Tt]{2}[^Pp]:\/\// for the RegExp. Otherwise, I agree with the other posters... using indexOf would be far easier (i.e., url.toLowerCase().indexOf('http://') !== 0).