Could not remove special charcters from string using Javascript [duplicate] - javascript

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

Related

Separate words by space and take into consideration that some contain apostrophe [duplicate]

This question already has answers here:
JavaScript split String with white space
(8 answers)
Closed last year.
In my input field I have sometimes words that contain apostrophe or need to be searched using quotes.
Example of searched word: "de l'article".
I need to separate them by space, so I will have "de" and "l'article".
I tried:
let word = document.getElementById("searchedWord").value.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
let x = new RegExp("(" + word.replace(/\s+[']+/g, "|") + ")", "ig");
It doesn't seem to work properly. Can anyone help me write this regular expression?
You should check out the split function :)
for example in order to split on every space
let x = "This is an example";
const myArray = x.split(' ');
let word = myArray[1]
word = 'This'
myArray = [This,is,an,example]

JavaScript how to strip/remove a char from string [duplicate]

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'

JavaScript replace() not working, get same result [duplicate]

This question already has an answer here:
JS regex replace not working
(1 answer)
Closed 5 years ago.
I'm trying to get only AN from this string, but replace function seems not working, I end up getting exactly the same string every time... any hits?
building = "\n AN ";
var temp = building.replace("\\W", "");
You need the meta sequence any non-word character \W and the global flag g for a continuing replacement.
var building = "\n AN ",
temp = building.replace(/\W/g, "");
console.log(temp);

Letters with apostrophe disappears [duplicate]

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 ...

Parsing regex in javascript [duplicate]

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

Categories

Resources