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

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

Related

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 substring if the substring is not prepended with a character Regex or other [duplicate]

This question already has answers here:
Regex for matching something if it is not preceded by something else
(3 answers)
How to make word boundary \b not match on dashes
(3 answers)
Closed 2 years ago.
I need to replace all instances of a string(needle) within another string(haystack), but only where the needle is not preceeded by a specific character, in this case '-'
var needle = 'secret';
var haystack = 'secret title date user -secret secret something.secret';
//Regex that needs to be adjusted
var result = haystack.replace(/secret/g, '');
//Desired result is
var desiredResult = ' title date user -secret something.';

How do I use JavaScript RegExp to replace last occurance of a string [duplicate]

This question already has answers here:
How to replace last occurrence of characters in a string using javascript
(3 answers)
Closed 3 years ago.
I have a JavaScript variable with string contents having commas (,). How do I replace the last comma (,) with 'and' using RegExp?
var band = "John, Paul, George, Ringo";
var beatles = band.replace(<reg-exp>, "and");
console.log(beatles); //outputs "John, Paul, George and Ringo"
You can use greedy .* at the start before comma and capture in a group to put it back in replacement:
str = str.replace(/^(.*),/, "$1 and");
RegEx Demo
.* will match longest string before matching last ,.
Another approach is to use negated character class in the end to match 0 or more non-comma characters after comma:
str = str.replace(/,\s*([^,]*)$/, " and $1");
RegEx Demo 2

How to use replaceAll() in JavaScript? [duplicate]

This question already has answers here:
How do I replace all occurrences of a string in JavaScript?
(78 answers)
Closed 4 years ago.
I have string and there are alot of character which is "\" so I want to remove all of them but when I use
string.replace("\\","");
it removes just first character. There is no replaceAll in reactnative. How can I do that ?
Use regex in your replace.
Example:
const string = "aaabbbcccaaa";
// Removes first "a" only
console.log(string.replace("a", ""));
// Removes all "a"
console.log(string.replace(/a/g, ""));
You have to use a regex with the g modifier whichs means globally, that will apply the regex as many times as necessary. You can use a tool like this to build and test your regex.
console.log('hello\\world\\'.replace(/\\/g, ''));

Prevent special characters in string [duplicate]

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

Categories

Resources