This question already has answers here:
Efficiently replace all accented characters in a string?
(23 answers)
Closed 6 years ago.
Here is my regex for validating a e-mailadress. But it keeps removing letters with a apostrophe. For example (Hélen, becomes Hlen)
var firstname = $("#FirstName").val().replace(/å/gi, "a").replace(/ä/gi,"a").replace(/ö/gi, "o").replace(/[^a-z0-9\s]/gi, '');
var lastname = $("#LastName").val().replace(/å/gi, "a").replace(/ä/gi, "a").replace(/ö/gi, "o").replace(/[^a-z0-9\s]/gi, '');
$("#Mail").val(firstname + "." + lastname + '#customer.Email');
I think that what you need is actually replace accented char with non accented version.
I found this solution here in SO: Remove accents/diacritics in a string in JavaScript
hope this helps
With : replace(/[^a-z0-9\s]/gi, '') you are replacing anything that isn't a non-accented letter , by an empty string.
That's why it is erased.
For that not to happen, you have to do with "é" the same thing you do with "å" or "ä", replace it with the non accented letter.
For example to replace "é", "è", "ê" and "ë" by e you can use replace(/[éèêë]/gi, "e") before using replace(/[^a-z0-9\s]/gi, '').
You'll have to do the same with "à" and "ô" etc ...
Related
This question already has answers here:
Remove not alphanumeric characters from string
(10 answers)
Regular expression to remove anything but alphabets and '[single quote]
(1 answer)
javascript regex to return letters only
(6 answers)
Ignoring invisible characters in RegEx
(2 answers)
Closed 2 years ago.
I am performing a validation in html text box which should pass only alphabets(a-z/A-Z) and few special characters like (*,& etc..). Otherwise it should show error some error.
I had written a JavaScript function which does the same.
function removeInvalidCharacters(selectedElement) {
if (selectedElement && typeof selectedElement.val() !== typeof undefined && selectedElement.val() != "") {
selectedElement.val(selectedElement.val().replace(/[\u0000-\u001F]|[\u007F-\u00A0]/g, "").replace(/\\f/g, "").replace(/%/g,""));
}
}
I am filtering selectedElement before passing to the function removeInvalidCharacters.
$("#name").val(toASCII($("#name").val()));
var selectedElement = $("#name").val();
But now I am facing a scenario in which empty characters, blank characters, invisible characters and whitespace characters are bypassing my regex. I could see some invisible characters are present in my name field. I want to replace these characters.
In further investigation I could found that Invisible characters - ASCII
characters mentioned in this link are the culprits. I need to have a regex to catch them and replace them.
Eg: AAAAAAAAAAAAAAAAAAAAAAA is the value in text field. Now if we check $("#name").val().length, it gives 24 ,even though we could see only 23 characters. I need to remove that hidden character.
Please help me with this scenario. Hope my query is clear
UPDATE:
var result = selectedElement.replace(/[\u200B-\u200D\uFEFF]/g, ''); fixed my problem.
Thank you all for the support.
If you want to allow only (a-z/A-Z) like you mention, try this:
str = str.replace(/[^a-zA-Z]/g, '');
Include the chars you want to keep instead of the ones you do not want, since that list may be incomplete
Otherwise look here: Remove zero-width space characters from a JavaScript string
const val = `AAAAAAAAAAAAAAAA**AAAAAAA`;
const cleaned = val.replace(/[^A-Za-z*]/g,"");
console.log(val.length,cleaned.length);
This question already has answers here:
List of all characters that should be escaped before put in to RegEx?
(7 answers)
Closed 3 years ago.
I want to replace every occurence of a question mark, point, comma etc. in a string called TextToSimplify but I keep getting the error as said in the title. What can I do about it
ToSimplifyText = ToSimplifyText.replace(/,/g, "");
ToSimplifyText = ToSimplifyText.replace(/./g, "");
ToSimplifyText = ToSimplifyText.replace(/!/g, "");
ToSimplifyText = ToSimplifyText.replace(/?/g, "");
ToSimplifyText = ToSimplifyText.replace(/'/g, "");
You can do all that stuff on one single replace, like this:
ToSimplifyText = "Hello. Dude! Aren't you ok?"
console.log(ToSimplifyText.replace(/[,.'?!]/g, ""));
Note, that if you want to replace a special character, you will have to escape it with \. Note also, this is not necessary when using a set of characters with [...], like in the previous example:
ToSimplifyText = "Hello. Dude! Aren't you ok?"
console.log(ToSimplifyText.replace(/\?/g, ""));
This question already has answers here:
Remove all special characters except space from a string using JavaScript
(13 answers)
Closed 4 years ago.
I need to remove all special characters from string using Javascript but its unable to remove. Please find my code below.
function checkString(){
var sourceString='a|"bc!#£de^&$f g';
var outString = sourceString.replace(/[`~!##$%^&*()|+\-=?;:'",<>\{\}\[\]\\\/]/gi, '');
console.log('sourcestring',outString);
}
Here I could not get the expected output. I am getting this abc£def g in console. Here I need to remove all special characters. Please help me to resolve this issue.
Use regex:
var sourceString='a|"bc!#£de^&$f g';
console.log("Before: " + sourceString);
sourceString = sourceString.replace(/[^a-zA-Z0-9 ]/g, "");
console.log("After: " + sourceString);
It essentially removes everything but alphabet and numbers (and spaces).
Remove every thing except numbers and letters.
var sourceString='a|"bc!#£de^&$f g';
// var outString = sourceString.replace(/[`~!##$%^&*()|+\-=?;:'",<>\{\}\[\]\\\/]/gi, '');
var outString = sourceString.replace(/[^a-zA-Z0-9]/g, '');
console.log('sourcestring',outString);
This question already has answers here:
How can I remove a character from a string using JavaScript?
(22 answers)
Closed 5 years ago.
I'm looking on how to remove a char from a string for example let's say i have "#22UP0G0YU" i want it to remove the # from it how would i do? I also have a small little other question too about how to make string upper case as well thanks in advance.
To remove a specific char I normally use replace, also good for a set of chars:
var str = '#22UP0G0YU';
var newString = str.replace('#', ''); // result: '22UP0G0YU'
To Uppercase, just use .toUpperCase();
var str = '#22UP0G0yu';
var newString = str.replace('#', '').toUpperCase(); // result: '22UP0G0YU'
This question already has answers here:
JavaScript Regex, where to use escape characters?
(3 answers)
Closed 8 years ago.
I'm unable to parse a regex. I've tested it from regexpal.com and regex101.com (by setting the expression as "(?:^csrf-token|;\s*csrf-token)=(.*?)(?:;|$)" and the test string as "__ngDebug=false; csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj") where it works.
The example is jsfiddle.
function getCookie(name) {
var s = "__ngDebug=false; csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj";
var regexp = new RegExp("(?:^" + name + "|;\s*"+ name + ")=(.*?)(?:;|$)", "g");
var result = regexp.exec(s);
return (result === null) ? null : result[1];
}
alert(getCookie("csrf-token"));
If however s is "csrf-token=b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj", then it works fine. Please tell me what's wrong.
The expected output is "b2ssOJ4jNOlPdmXAHn4CORPvFoO4Ngsupej25tdj", and there is no input (the string to be tested is 's').
Change
"|;\s*"
to
"|;\\s*"
^
The thing is, you are constructing the RegExp by passing in a string via the constructor, so you need to follow the rule of escaping in string literal. In JavaScript, "\s" is recognized as a single character string, with lowercase s. To specify \, you need to escape it.
You should escape \s
var regexp = new RegExp("(?:^" + "csrf-token" + "|;\\s*"+ "csrf-token" + ")=(.*?)(?:;|$)", "g");
^