regex string correct but not working - javascript

I have a regex expression which is
function checkNum(form) {
var regex = "/^\d+/";
if (!form.value.match(regex)) {
alert('Please Enter value again');
form.value = "";
}
}
but it is not working.
I have entered this regex expression /^\d+$/ in this website http://www.regular-expressions.info/javascriptexample.html but it doesnt seem to work too in this website. I am pretty sure this expression is correct

Regex literal is /^\d+/ without quotes.
function checkNum(form) {
var regex = /^\d+$/; //Pure digits from start to end
if (!form.value.match(regex)) {
alert('Please Enter value again');
form.value = "";
}
}
Demo http://jsfiddle.net/KvuaM/
If you pass the string "/^\d+/" to .match() you will get a regex equivalent of /\/^d+\// because of the implicit conversion and invalid "\d" escape.
new RegExp("/^\d+/").source
//"/^d+/"

You have slashes in the string, which are interpreted as literals.
Either remove them from the string, or use them in order to create a regular expression
"^\d+"
or
/^\d+/

Related

Incorrect regex expression for Javascript

function palindrome(str) {
//Clean up string
var re = '/[^a-z0-9]/gi';
str=str.toLowerCase();
str=str.replace(re, '');
console.log(str);
//Reverse string and check
var pstr = str.split('').reverse().join('');
if (pstr === str){
return true;
}
return false;
}
palindrome("__Eye");
I'm trying to test a palindrome statement. I'm using https://regex101.com/ to test my regEx statements using sample statements
The function above is an attempt to check if a string value is a palindrome, return true if it is, return false if its not
Palindromes are things like race car where its spelled the same forward and backward
My regex expression is '[^a-z0-9]/gi' which selects all punctuation marks, commas, and spaces in order to delete them using a replace prototype string method. On testing the regex expression it looks fine, see below
problem:
Can someone shed light on what I am doing wrong here? The problem I have is that I am console.log(str) and its not reflecting the correct output. e.g.
__eye input should result in eye output but is not
repl to test code here https://repl.it/JVCf/21
EDIT PROBLEM SOLVED:
Its var re = /[^a-z0-9]/gi; NOT var re = '/[^a-z0-9]/gi';
RegEx Pattern is NOT a string
From the MDN documentation:
There are 2 ways to create a RegExp object: a literal notation and a constructor. To indicate strings, the parameters to the literal notation do not use quotation marks while the parameters to the constructor function do use quotation marks. So the following expressions create the same regular expression:
/ab+c/i;
new RegExp('ab+c', 'i');
new RegExp(/ab+c/, 'i');
The different methods have their different pros and cons.
function palindrome(str) {
//Clean up string
var re = /[^a-z0-9]/g; // note RegEx pattern is not a string
str=str.toLowerCase();
str=str.replace(re, '');
console.log(str);
//Reverse string and check
var pstr = str.split('').reverse().join('');
if (pstr === str){
return true;
}
return false;
}
palindrome("__Eye");

Regex Expression validation in javascript

Regex to check the first character is in uppercase and allow only alphanumeric,not allow the special charcter.
Thank you Advance
function checkName(val) {
var alpha = document.getElementById(val).value;
var filter = /^[a-zA-Z0-9 ]*$/;
if (!filter.test(alpha)) {
alert("Please Enter Alphanumeric Only");
return false;
}
return true;
}
i Used This its working properly for checking alphanumeric but for
first character uppercase its not working
where can i modify my regex expression or any solution.
try
/^[A-Z][a-zA-Z0-9]+/
For example
/^[A-Z][a-zA-Z0-9]+/.test("Asd");

Javascript date pattern

I am trying to create JavaScript pattern to see if input value is date, my date format is 2015/Jan/01, I have tried doing this \d{4}/\[A-Za-z]{3}/\d{1,2} but that did not work, Please help me in fixing it.
function SetValue() {
// var newdata = document.getElementById('txtCellEditor').value;
var myString = txtCellEditor.value;
if (myString.match(\d{4}/\[A-Za-z]{3}/\d{1,2})) {
alert("'myString' is date.");
}
Your regex is incorrect
Use delimiters of regex i.e. / at the start and end of regex
Use ^ and $ to check exact match instead of substring
Escape the / by preceding them with \
No need to escape [
Use test() instead of match
Visualization
Demo
var regex = /^\d{4}\/[a-z]{3}\/\d{1,2}$/i;
function SetValue() {
var myString = document.getElementById('date').value;
if (regex.test(myString)) {
console.log("'myString' is date.");
} else {
console.log("'myString is not date.");
}
}
<input type="text" id="date" onkeyup="SetValue()" />
Most of the actual regular expression is fine, but you've forgotten to make it a regular expression by putting it in /s! (And you've escaped one of the / [but backward], but not the other.)
if (myString.match(/\d{4}\/[A-Za-z]{3}\/\d{1,2}/)) {
// ^ ^^ ^^ ^
// regex quote-/ \--escaped--/ \-- regex quote

Javascript Regular Expressions are not working; always returns false

I need to write a regular expression that will check that the strings matches the format 'ACT' followed by 6 digits eg. 'ACT123456'
Though it looks quite simple, none of my options work; the function always returns false.
I tried the following combinations:
Pure regexpression literals
var format = /^ACT\d{6}$/;
var format = /^ACT[0-9]{6}$/;
Or using RegExp object with double escaping (eg. \\d) and with single escaping (\d)
var format = new RegExp("^ACT\\d{6}$");
var format = new RegExp("^ACT[0-9]{6}$");
My function for testing is:
function testPattern(field, pattern) {
if (!pattern.test(field)) {
return false;}
else {
return true;
}}
var format = /^ACT\d{6}$/;
works fine but the string must be ACT123456 exactly with nothing preceding it or following it
eg 'ACT123456 ' fails
use
/ACT\d{6}/
to allow more tolerance or strip the whitespace from the string first
var testString = "ACT123456"; // string to test
// pattern as a regex literal
var pattern = /^ACT[0-9]{6}$/g;
console.log(testString.match(pattern)); // output: ["ACT123456"]
Thanks you all guys for answers and feedback!
After being stuck with regular expressions, I realized that my problem with that I am not using field.value in my function.
So, the problem is with the function that must be:
function testPattern(field, pattern) {
if (!pattern.test(field.value)) {
return false;}
else {
return true;
}}

RexExp in javascript dont match a number inside a string

Im learning Regular Expresions in Javascript and there is a thing that i dont understand.
The following regexp should match any string from a to z but if I add a number it says that is correct
var patron = /[a-zA-Z]/;
var regex = new RegExp(patron);
var v= "hello word 512";
if(v.match(regex))
{
//should not match but it does
}else
{
objInput.style.color = "red";
}
And them i tried this:
var patron = /[a-zA-Z\D]/;
var regex = new RegExp(patron);
var v= "hello word 512";
if(v.match(regex))
{
//should not match but still dont work
}else
{
objInput.style.color = "red";
}
And also, parentheses are not being match
var patron = /[a-zA-Z\"\']/;
var regex = new RegExp(patron);
var v= "hello word 512";
if(v.match(regex))
{
//it match whenever the double quoute it followed by the single quoute'
}else
{
objInput.style.color = "red";
}
About the first example you provided, your regex /[a-zA-Z]/ checks for any character in the input string. Since it finds h in your input string, it returns true.
What you need to do is place start and end anchors, ^ and $ in your regex. New regex would look like this:
/^[a-zA-Z]+$/
You can make changes to all you regex accordingly.
To match parentheses, you need to escape them with a backslash. \( would match (, and \) would match ).
You should match the whole string, using the ^ (matches the beginning of the string) and $ (matches the end of the string) operators, for example:
/^[a-zA-Z\s]+$/.test("any string followed by numbers! 555") // will return false
This will not allow anything else than a-z chars and spaces in your string.
the match function seeks for at least ONE match in your case this is 1st symbol which is a char.
if you want ONLY chars then use /[a-zA-Z]/.test("your string")

Categories

Resources