Prevent special characters in string [duplicate] - javascript

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)));

Related

what is the regex for the following part? [duplicate]

This question already has answers here:
Get Filename from URL and Strip File Extension
(5 answers)
Closed 1 year ago.
src/insp/custom/extractThis1.q.ts
what would be the regex for extractThis1 ?
I have tried like this /\/(.*).q.ts/gm but didn't work
You can match until the last occurrence of /, and then use a capture group to match any char except a . followed by .q.ts
^.*\/([^.]+)\.q\.ts$
Regex demo
const s = "src/insp/custom/extractThis1.q.ts";
const regex = /^.*\/([^.]+)\.q\.ts$/;
const m = s.match(regex);
if (m) {
console.log(m[1]);
}

Replace Space with Dash and Remove Semicolon [duplicate]

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)

javascript - replace() delete all the repeat letter in a string [duplicate]

This question already has answers here:
Remove consecutive duplicate characters in a string javascript
(5 answers)
Closed 2 years ago.
I want to delete all the repeat letter in a string with javascript.
mot = "message in a bottle";
mot = mot.replace(/[\w]{2,}/i, '');
console.log(mot)
// result wish : "mesage in a botle"`
Thank you for your help.
You can group the word characters and then use a backreference to check if the same character follows that grouped character (with \1). You can replace all found occurrences with the group using $1. Ensure that you use the global flag (/g) to match all occurrences:
const mot = "message in a bottle";
const res = mot.replace(/(\w)\1/ig, '$1');
console.log(res);

Match once but not repetitions [duplicate]

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:
/^(?!.*\.\.).*$/

Javascript regex for accepting only number without "," or "." character [duplicate]

This question already has answers here:
Regex to check whether a string contains only numbers [duplicate]
(21 answers)
Closed 5 years ago.
I'm looking for a regex javascript expression for allowing only numbers at this format 1234. Not 1,234 or 1.234. I'm having this regex now but it does not seem to work properly /[a-z]/i
var reg = /^\d+$/;
should do it. The original matches anything that consists of exactly one digit.

Categories

Resources