jQuery - Replace all parentheses in a string - javascript

I tried this:
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace("(", "").replace(")", "");
It works for all double and single quotes but for parentheses, this only replaces the first parenthesis in the string.
How can I make it work to replace all parentheses in the string using JavaScript? Or replace all special characters in a string?

Try the following:
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(|\)/g, "");
A little bit of REGEX to grab those pesky parentheses.

You should use something more like this:
mystring = mystring.replace(/["'()]/g,"");
The reason it wasn't working for the others is because you forgot the "global" argument (g)
note that [...] is a character class. anything between those brackets is replaced.

You should be able to do this in a single replace statement.
mystring = mystring.replace(/["'\(\)]/g, "");
If you're trying to replace all special characters you might want to use a pattern like this.
mystring = mystring.replace(/\W/g, "");
Which will replace any non-word character.

You can also use a regular experession if you're looking for parenthesis, you just need to escape them.
mystring = mystring.replace(/\(|\)/g, '');
This will remove all ( and ) in the entire string.

Just one replace will do:
"\"a(b)c'd{e}f[g]".replace(/[\(\)\[\]{}'"]/g,"")

That should work :
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(/g, "").replace(/\)/g, "");

That's because to replace multiple occurrences you must use a regex as the search string where you are using a string literal. As you have found searching by strings will only replace the first occurrence.

The string-based replace method will not replace globally. As such, you probably want to use the regex-based replacing method. It should be noted:
You need to escape ( and ) as they are used for group matching:
mystring= mystring.replace(/"/g, "").replace(/'/g, "").replace(/\(/g, "").replace(/\)/g, "");

This can solve the problem:
myString = myString.replace(/\"|\'|\(|\)/)
Example

Related

replace single quote in middle of string using regex

Have a string:
stringName= "'john's example'"
Need to do a string.replace to remove the single quote in the middle of the string, not the first and last otherwise will break my javascript
have tried stringName.replace("/.'./","") to replace only the single quote in the middle of the string but does not work
Help is very appreciated! :)
Use (^'|'$)|' as matching regular expression:
stringName = "'john's e'xam'ple'";
console.log(
stringName.replace(/(^'|'$)|'/g, '$1')
);
First thing is you aren't doing a regex replace, you are replacing a string which looks like /.'./ (because of the " in the first argument). Secondly, the regex you're doing is only going to be looking for a single character (.) then a single quote, then another character. What you might want to do is something like stringName.replace(/(.+)'(.+)/, "$1$2")
Use split, join after stripping of first and last character
var f1 = (str) => str.charAt(0) + str.split("'").join("") + str.slice(-1);
f1( "'john's exa''mp'le'" ); //'johns example'

Replacing, regex, javascript

Got this string:
'test',$, #207
I need to remove spaces which have a commma before
So the result will be: 'test',$,#207
Tried this:
replace(/\/s,]/g, ',')
Not working. Any ideas?
To replace only spaces and not other whitespaces use the following regex.
Regex: /, +/g
Explanation:
, will search for comma.
+ will search for multiple spaces.
And then replace by , using replace(/, +/g, ',')
Regex101 Demo
JSFiddle demo
Since your pattern is simple you can just do this .split(', ').join(',')
I need to remove spaces which have a commma afterwards
No, your example says the opposite. That is, you want to remove spaces that have a comma before them.
In either case, the error in your expression is the "]".
replace(/\/s,/g, ',')
Does what you say you want to do, and
replace(/,\/s/g, ',')
Does what the example says.
The other answer is right, though - just use replace(' ,', ''); you need no regex here.
I think you meant comma that have whitespace afterwards:
stringVar = "'test',$, #207";
replace('/\,\s/g', stringVar);
\, means , literally and \s means whitespace.
You can test javascript regex and know a little more about the modifiers and stuff at regex101.
replace(new RegExp(find, ', '), ',');
For all whitespaces which have a "," before
var str = "test,$, #207, th, rtt878";
console.log(str.replace(/\,\s+/g,","));
var str = "test,$, #207";
console.log(str.replace(/\,\s+/g,","));

javascript check if string contains any symbols

I have the following set of symbols:
var a = '|\/~^:,;?!&%$#*+';
How can I check is the following string contains any of those symbols?
var b = 'avguybdf';
As suggested, regular expressions will work.
b.match(/[|\\/~^:,;?!&%$#*+]/);
EDIT: I originally used the method here https://stackoverflow.com/a/6969486/2044733 to escape the string but because of the grouping, only the backslash character needs to be escaped.
The "/" at the beginning and end of the string are the delimiters for regular expressions in javascript, and "[]" are used to group the characters. In case you're wondering how this works.
Use RegEx
Check the how to use regex # Javascript RegEx
Try one of the following examples that use regular expressions:
http://www.webdeveloper.com/forum/showthread.php?264705-Best-way-to-check-for-multiple-characters-in-a-string
http://tjvantoll.com/2013/03/14/better-ways-of-comparing-a-javascript-string-to-multiple-values/
Use RegEx. You can use test() or exec(). Read more here: http://www.w3schools.com/jsref/jsref_obj_regexp.asp

Javascript : replace reg exp

I want to replace all symbols that aren't letters by -, but my code doesn't work :
$reg = '/[^a-zA-Z]+/g';
$txt = $txt.replace($reg, '-');
What am I doing wrong?
Regular Expressions in JavaScript are not strings.
reg = /[^a-z]+/gi;
txt = txt.replace(reg, '-');
You don't need to place quotes around them.
You need to un-quote the regex string so it's treated as a regular expression literal, so you get this:
$reg = /[^a-zA-Z]+/g;
$txt = $txt.replace($reg, '-');
Regular expressions in JavaScript don't need to be quoted as strings unless using the new Regexp() notation; in the above example, it is now a regular expression literal, which isn't treated as a string but a piece of regex to be used in .replace().
do not use quote on regex. Without quotes, they are RegEx object. With quotes they are just string.
Use,
$reg = /[^a-zA-Z]+/g;
Remove the quotes from around your regex.
If it is your intention for multiple non-alpha characters in a row to be replaced with a single hyphen your regex will then work. If you want multiple non-alpha characters to be replaced with multiple hyphens then you should also remove the + sign.

JavaScript regex with escaped slashes does not replace

Do i have to escape slashes when putting them into regular expression?
myString = '/courses/test/user';
myString.replace(/\/courses\/([^\/]*)\/.*/, "$1");
document.write(myString);
Instead of printing "test", it prints the whole source string.
See this demo:
http://jsbin.com/esaro3/2/edit
Your regex is perfect, and yes, you must escape slashes since JavaScript uses the slashes to indicate regexes.
However, the problem is that JavaScript's replace method does not perform an in-place replace. That is, it does not actually change the string -- it just gives you the result of the replace.
Try this:
myString = '/courses/test/user';
myString = myString.replace(/\/courses\/([^\/]*)\/.*/, "$1");
document.write(myString);
This sets myString to the replaced value.
/[\/]/g matches forward slashes.
/[\\]/g matches backward slashes.
Actually, you don't need to escape the slash when inside a character class as in one part of your example (i.e., [^\/]* is fine as just [^/]*). If it is outside of a character class (like with the rest of your example such as \/courses), then you do need to escape slashes.
string.replace doesn't modify the original string. Instead, a returns a new string that has had the replacement performed.
Try:
myString = '/courses/test/user';
document.write(myString.replace(/\/courses\/([^\/]*)\/.*/, "$1"));
Note, that you don't have to escape / if you use new RegExp() constructor:
console.log(new RegExp("a/b").test("a/b"))

Categories

Resources