How to solve the "Invalid regular expression: /?/: Nothing to repeat" problem [duplicate] - javascript

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

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]

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

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

How to replace the backward slash from string? [duplicate]

This question already has answers here:
JavaScript: A BackSlash as part of the string
(3 answers)
Closed 4 years ago.
What is wrong with the following code?
Expected output : substr1#substr2#substr3
var str = "substr1\substr2\substr3"
// it works if I use the double slash "\\" in thestring but not with single.
console.log(str.replace(/\\/g, "#"));
Your initial string itself do not have a backslash. To verify check the snippet below:
var str = "substr1\sustr2\substr3"
console.log(str);
The actual output you expect can be obtain by first escaping the backslash and then replacing it with #:
var str = "substr1\\sustr2\\substr3"
console.log(str.replace(/\\/g, "#"));

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'

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

Categories

Resources