This question already has answers here:
Regex that does not allow consecutive dots
(3 answers)
Regex for not allowing consecutive dots or underscores
(3 answers)
Regex for not containing consecutive characters
(4 answers)
Closed 4 years ago.
I have the following code to match:
String(s) in which . is not present consecutively, it can be alternatively
let strArr = [
'#foo3.5', // true
'#bar34..34', // false
'.', // true
'#ipv4-1.1.1.1' // true
];
const re = /^([^.]*\.[^.]*)+$/;
strArr.map(
(val, idx) => console.log(`${idx}: ${re.test(val)}`)
);
But, the above code also matches #bar34..34, which is not desired. I tried changing the * meta-character in my pattern to +, but then, it fails to match . and #ipv4-1.1.1.1 strings.
Also, I want my regex to be small, because it is a part of a very long regex (you can suppose it an Email ID regex). So, what should be the required regex?
Match a whole string with no consecutive dots:
/^(?!.*\.\.).*$/
Related
This question already has answers here:
How to replace all characters in a string except first and last characters, using JavaScript
(6 answers)
Regex to mask characters except first two and last two characters in Java
(4 answers)
Closed 1 year ago.
I need to hide some info of a string. This string is a card number so I want to save only the first 4 and last 4 digits and the rest is going to be 'X'
For example if I have this card 12345678901234 I want it to be 1234XXXXXX1234
What regular expresion could I use to match those characters?
const hide = card => card.replace(/First4AndLast4/g, 'X')
const hide = card => card.slice(0, 4) + "X".repeat(card.length-8) + card.slice(-4)
console.log(hide('111111111111'))
This question already has answers here:
Replace comma or whitespace with hyphen in same string
(3 answers)
Closed 2 years ago.
This regex replaces empty spaces with a dash replace(/\s+/g, "-").
If I also want to remove any semicolons, how do I go about adding that to the above regex?
Basically, I want this string hello; how are you to be hello-how-are-you
You could add ; in [] - which means groups and ranges
/[\s;]+/g
const str = "hello; how are you"
const res = str.replace(/[\s;]+/g, "-")
console.log(res)
This question already has answers here:
Regular Expression only match if String ends with target
(3 answers)
Closed 5 months ago.
I have two strings
"track/bugfix merged to 'master'"
"track/bugfix merged to 'track/clonemaster'"
I want regex to match string ends with 'master' only
can anyone help me on this?
To get the end of a string, use the $ anchor, this signifies the end of a line.
So, the regular expression that you are looking for is as follows:
/'master'$/gi
const matches = [
"track/bugfix merged to 'master'",
"track/bugfix merged to 'track/clonemaster'"
].map(testString => !!testString.match(/'master'$/gi))
console.log(matches)
This question already has answers here:
Regex to Match Symbols: !$%^&*()_+|~-=`{}[]:";'<>?,./
(7 answers)
Closed 3 years ago.
I would like to know regex code to check whether it contains Special characters other than hypen and forwardslash in javascript
function containsSpecialCharacters(str){
var regex = /[~`!#$%\^&*+=\-\[\]\';,/{}|\\":<>\?]/g;
return regex.test(str);
}
var result = containsSpecialCharacters("sample"); // false
var result = containsSpecialCharacters("sample-test"); // false
var result = containsSpecialCharacters("sample++"); // true
var result = containsSpecialCharacters("/sample/test"); // false
You can use a normal regex to search for all ASCII special characters, like this one, and then just remove the hypen and backslash:
/[!$%^&*()_+|~=`{}\[\]:";'<>?,.]/
If you want to avoid any other special character, you just have to remove it from the regex.
You can test this regex here: https://regex101.com/r/gxYiGp/2
If you need to support only English characters, it would be far easier to list the exceptions than those you wish to match:
/[^\w\s\\\-]/.test(myString);
As noted, though, this will fail if you need to support international characters.
you can just negate it so it looks shorter, like this :
[^\s\w-]
This question already has answers here:
Regular expression for excluding special characters [closed]
(11 answers)
Closed 4 years ago.
I have this regular expression and I need to prevent a password having any of these symbols: !##$%^&*()_, it is working for !##$% but not for this password !##$%D.
$scope.matchPatternPassword = new RegExp("[^!##$%^&*()_]$");
Your regex was only checking for any of those symbols at the end of the string, that's why the one ending in a letter was working.
The regex should be:
$scope.matchPatternPassword = /^[^!##$%^&*()_]+$/;
This matches any string that doesn't have any of those characters.
Here's a working example: https://regexr.com/3nh9f
const regex = /^[^!##$%^&*()_]+$/;
const passwords = [
'!##$%',
'!##$%D',
'yes',
'valid-password',
'im-valid-too',
'invalid$',
'super!-invalid',
'mypassword'
];
console.log(passwords.filter(password => regex.test(password)));