Remove single quotes and last comma in jquery string using Regex [duplicate] - javascript

This question already has answers here:
JavaScript REGEX Match all and replace
(2 answers)
Reference - What does this regex mean?
(1 answer)
Closed 3 years ago.
I have a data
'abc','def','ghi',
I want to remove the single quote on all character and want to remove only the last comma, example like below
abc,def,ghi
How would i achieve that using regex for javascript?
I tried using this regex
.replace(^\'|,\s*$,"");
But seems like it is only removing the first quote as shown below
abc','def','ghi',
I am not very good in regex, i appreciate any help that i can get. Thanks

try this:
.replace(/\'|,$/g, "");
the ^ at the beggining made the regexp to only match the quote at the beggining of the string, also you have to add the g to keep looking after the first match

There is an easy way, use the $ operator
.replace(/'|,$/g, '')

Can you please check replace(/'|(,)$/g," ") and it should work.

Related

invalid Regex group [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I'm trying to create the following regex using Javascript.
(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)
However, by doing this it gives me invalid group error in the console.
regExp = new RegExp("(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)", "gi");
I don't understand where the problem comes from exactly. I appreciate the help.
Thank you
EDIT: After some research I found that Javascript does not support lookbehinds.
So the error comes from (?<!\\).
Refer this newly asked question to find an alternative way to do the same job.
How to check for odd numbers of backslashes in a regex using Javascript?
If your expression isn't dynamic, just use a literal:
var regExp = /(?<!\\)(?:\\{2})*\\(?!\\)([5-9]|[1-9]\d)/gi;
The problem is that your escape sequences \\ inside the string end up rendering \ characters inside the regEx, which in turn end up escaping brackets they shouldn't, resulting in unterminated groups.

How to use end of string in square brackets in javascript regex? [duplicate]

This question already has answers here:
Using $ anchor inside a character class does not work
(2 answers)
Closed 3 years ago.
In js regex, I have
[\.\?!][\s$]
what I want to do is match
literal dot, or literal question mark or explanation mark
then
either 1 whitespace character or, be at the end of the string.
However the regex above, tries to match the literal $.
Does anyone know how to fix this?
Thanks
Try this Regex:
[.?!](?:\s|$)
Click for Demo
Explanation:
[.?!] - matches either a . or a ? or a ! literally
(?:\s|$) - matches either a white-space or the End-of-line

How to fix my regex? [duplicate]

This question already has answers here:
Matching a Forward Slash with a regex
(9 answers)
Closed 8 years ago.
I tried making a regex expression to search for in the string named 'patt'. Unfortunately the following gives an error in dreamweaver:
patt.search(/.*://.*waw\d.omegle.com/);
I need to get this pattern working. What am i doing wrong here?
You need to escape the / in the pattern as
patt.search(/.*:\/\/.*waw\d\.omegle\.com/);
Also escape . for more saftey as . alone could match anything in regex
Example
var patt = "http://asdfwaw1.omegle.com";
patt.search(/.*\/\/.*waw\d\.omegle\.com/);
=> True
because it thinks the / in // is the end of the reg exp. If you look at the coloring in your code above you can see the brown color ends at the first slash. You need to escape it.
/.*:\/\/.*waw\d.omegle.com/;

replace does not replace every character in a sentence [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Why does javascript replace only first instance when using replace?
How do I replace all occurrences of “/” in a string with “_” in JavaScript?
I want to replace every - in a sentence but it only replace the first -. Here is my code:
var string = 'this-is-a-line-of-words';
alert(string.replace('-', '/'));​
Why does it only replace the first character I want to replace? jsFiddle demo.
Thanks in advance.
Use a global regex:
string.replace(/-/g, '/')
Please use string.replace(/-/g, '/'). And check this doc please.

How can I use regex to replace all $#8211; with – in javascript? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
replace all occurrences in a string
I found this question/answer:
Use JavaScript regex to replace numerical HTML entities with their actual characters
I just need to replace the one entity though. How can I match that specific pattern with a regex?
I don't know much about regex so I've done this:
.replace('–', '–')
But it obviously only replaces the first instance.
Thanks,
Thomas
The replace method only replaces the first occurance when you are using a string. Use a regular expression, so that you can specify the global flag g:
.replace(/–/g, '–')
.replace(/–/g, '–')
the g flag means global so it replaces all instances.

Categories

Resources