I just need a really simple javascript regex test to make sure the last character of a given string is a number 0-9. I keep finding different variations of this in my searches but none that really match the simplicity of what I need. So for instance
var strOne = "blahblahblahblah"; // === false
var strTwo = "blahblahblahblah///"; // === false
var strThree = "blahblahblahblah123"; // === true
Any help is appreciated... I'm still trying to wrap my head around the rules of regex.
\d$ should do it.
\d - digit character class
$ - must match end of string
Tests:
/\d$/.test('blahblahblahblah'); // false
/\d$/.test('blahblahblahblah///'); // false
/\d$/.test('blahblahblahblah123'); // true
Try this regex /\d$/
var strOne = "blahblahblahblah";
var strTwo = "blahblahblahblah///";
var strThree = "blahblahblahblah123";
var regex = /\d$/;
console.log(regex.test(strOne)); // false
console.log(regex.test(strTwo)); // false
console.log(regex.test(strThree)); // true
This regex is pretty safe, give this a go: /\d+(\.\d+)?$/
Related
I have a string like this
[network-traffic:src_port =
and I want to check it it ends with = or == or !=
I have a regex like this
[^=]*={1}
just to start and now when feed it with ssss=== it matches and in the first step I am failing as 3 = is also matching though I need only 1 or 2 equality to be matched
what is the best way to achieve the above?
you can use the following regex ^[^=]*(?:={1,2}|!=)$ it breaks down as follows
match the start of the line
match 0 or more chars which are not an =
match 1 or 2 = OR match !=
match the end of the line
How about this?
function validate(str) {
return /(?<!.*=)([=!])?=$/.test(str)
}
console.log(validate('[network-traffic:src_port =')); // True
console.log(validate('[network-traffic:src_port ==!=')); // True
console.log(validate('ssss=== it')); // False
console.log(validate('ssss=== it===')); // False
I am using the code below to find a match for a plus sign but it keeps returning false. I am not sure what I am doing wrong. Any help will be really appreciated it. Thanks!
var str = '+2443';
var result = /d\+1/.test(str);
console.log(result); // true
var str = '+2443';
var result = /\+/.test(str);
console.log(result); // true
Your /d\+1/ regex matches the first occurrence of a d+1 substring in any string.
To check if a string contains a +, you do not need a regex. Use indexOf:
var str = '+2443';
if (~str.indexOf("+")) {
console.log("Found a `+`");
} else {
console.log("A `+` is not found");
}
A regex will be more appropriate when you need to match a + in some context. For example, to check if the string starts with a plus, and then only contains digits, you would use
var str = '+2443';
var rx = /^\+\d+$/;
console.log(rx.test(str));
where ^ assets the position at the end of the string, \+ matches a literal +, \d+ matches 1+ digits and the $ anchor asserts the position at the end of the string.
I'm trying to validate a form using regular expressions, the conditions are:
It has to be a numeric value
It CAN have up to three decimal places(0,1,2 are allowed too)
It has to be divided by a comma(,)
I already got it to work using HTML5-Patterns with this:
pattern='\d+(,\d{1,3})?'
Since patterns are not supported by IE9, I tried doing it with js:
var numPattern = /\d+(,\d{1,3})?/;
if(!numPattern.test(menge.val()))
{
checkvalidate = false;
}
Where did I go wrong?
Examples
valid: 1,234 ; 2,00 ; 5 ; 0,1
invalid: 1,2345 ; 2.00 ; 56a
You'll need to start your regex with ^ and end it with $ to make sure the entire input string/line is matched.
/^\d+(,\d{1,3})?$/
Here's a "demo" in which all your examples are valid/invalid:
https://regex101.com/r/oP5yJ4/1
(Using regex101.com to debug your regular expression patterns is often very useful)
Note that: (without ^ and $)
var pattern_without = /\d+(,\d{1,3})?/;
pattern_without.test("56a") === true; // matches, but only "56"
pattern_without.test("1,2345") === true; // matches, but only "1,234"
but: (with ^ and $)
var pattern_with = /^\d+(,\d{1,3})?$/;
pattern_with.test("56a") === false; // no match
pattern_with.test("1,2345") === false; // no match
You can use this regex:
/^\d+(?:,\d{1,3})*$/
RegEx Demo
Try this expression:
\d+(,\d{3})*([.]\d{1,3})?
Valid examples:
1,200.123
1,200.12
1,200.1
1.123
1,200,222
1,200,002
You can use the RegExp object.
var str = "123545,123";
var patt = new RegExp("/^(?:\d*\,\d{1,3}|\d+)$/");
var res = patt.test(str);
After execution, res will be true, since str matches the pattern you're looking for,
I'm working on a simple password validator and wondering if its possible in Regex or... anything besides individually checking for each character.
Basically if the user types in something like "aaaaaaaaa1aaaaa", I want to let the user know that the character "1" is not allowed (This is a super simple example).
I'm trying to avoid something like
if(value.indexOf('#') {}
if(value.indexOf('#') {}
if(value.indexOf('\') {}
Maybe something like:
if(/[^A-Za-z0-9]/.exec(value) {}
Any help?
If you just want to check if the string is valid, you can use RegExp.test() - this is more efficient that exec() as it will return true when it finds the first occurrence:
var value = "abc$de%f";
// checks if value contains any invalid character
if(/[^A-Za-z0-9]/.test(value)) {
alert('invalid');
}
If you want to pick out which characters are invalid you need to use String.match():
var value = "abc$de%f";
var invalidChars = value.match(/[^A-Za-z0-9]/g);
alert('The following characters are invalid: ' + invalidChars.join(''));
Although a simple loop can do the job, here's another approach using a lesser known Array.prototype.some method. From MDN's description of some:
The some() method tests whether some element in the array passes the test implemented by the provided function.
The advantage over looping is that it'll stop going through the array as soon as the test is positive, avoiding breaks.
var invalidChars = ['#', '#', '\\'];
var input = "test#";
function contains(e) {
return input.indexOf(e) > -1;
}
console.log(invalidChars.some(contains)); // true
I'd suggest:
function isValid (val) {
// a simple regular expression to express that the string must be, from start (^)
// to end ($) a sequence of one or more letters, a-z ([a-z]+), of upper-, or lower-,
// case (i):
var valid = /^[a-z]+$/i;
// returning a Boolean (true/false) of whether the passed-string matches the
// regular expression:
return valid.test(val);
}
console.log(isValid ('abcdef') ); // true
console.log(isValid ('abc1def') ); // false
Otherwise, to show the characters that are found in the string and not allowed:
function isValid(val) {
// caching the valid characters ([a-z]), which can be present multiple times in
// the string (g), and upper or lower case (i):
var valid = /[a-z]/gi;
// if replacing the valid characters with zero-length strings reduces the string to
// a length of zero (the assessment is true), then no invalid characters could
// be present and we return true; otherwise, if the evaluation is false
// we replace the valid characters by zero-length strings, then split the string
// between characters (split('')) to form an array and return that array:
return val.replace(valid, '').length === 0 ? true : val.replace(valid, '').split('');
}
console.log(isValid('abcdef')); // true
console.log(isValid('abc1de#f')); // ["1", "#"]
References:
JavaScript conditional operator (assessment ? ifTrue : ifFalse).
JavaScript Regular Expressions.
String.prototype.replace().
String.prototype.split().
RegExp.prototype.test().
If I understand what you are asking you could do the following:
function getInvalidChars() {
var badChars = {
'#' : true,
'/' : true,
'<' : true,
'>' : true
}
var invalidChars = [];
for (var i=0,x = inputString.length; i < x; i++) {
if (badChars[inputString[i]]) invalidChars.push(inputString[i]);
}
return invalidChars;
}
var inputString = 'im/b#d:strin>';
var badCharactersInString = getInvalidChars(inputString);
if (badCharactersInString.length) {
document.write("bad characters in string: " + badCharactersInString.join(','));
}
I am building a very basic profanity filter that I only want to apply on some fields on my application (fullName, userDescription) on the serverside.
Does anyone have experience with a profanity filter in production? I only want it to:
'ass hello' <- match
'asster' <- NOT match
Below is my current code but it returns true and false on in succession for some reason.
var badWords = [ 'ass', 'whore', 'slut' ]
, check = new Regexp(badWords.join('|'), 'gi');
function filterString(string) {
return check.test(string);
}
filterString('ass'); // Returns true / false in succession.
How can I fix this "in succession" bug?
The test method sets the lastIndex property of the regex to the current matched position, so that further invocations will match further occurrences (if there were any).
check.lastIndex // 0 (init)
filterString('ass'); // true
check.lastIndex // 3
filterString('ass'); // false
check.lastIndex // now 0 again
So, you will need to reset it manually in your filterString function if you don't recreate the RegExp each time:
function filterString(string) {
check.lastIndex = 0;
return check.test(string);
}
Btw, to match only full words (like "ass", but not "asster"), you should wrap your matches in word boundaries like WTK suggested, i.e.
var check = new Regexp("\\b(?:"+badWords.join('|')+")\\b", 'gi');
You are matching via a substring comparison. Your Regex needs to be modified to match for whole words instead
How about with fixed regexp:
check = new Regexp('(^|\b)'+badWords.join('|')+'($|\b)', 'gi');
check.test('ass') // true
check.test('suckass') // false
check.test('mass of whore') // true
check.test('massive') // false
check.test('slut is massive') // true
I'm using \b match here to match for word boundry (and start or end of whole string).