How to fix my regex? [duplicate] - javascript

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

Related

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

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.

Find String that starts and ends with a specific char [duplicate]

This question already has answers here:
Regex to match a string with specific start/end
(3 answers)
Closed 4 years ago.
I would like to find a string that starts and ends with a specific special character.
I tried the following regex but its not working:
(\#*\.|\&)[A-Za-z]+\.*#
I want to find any string that starts with #* and ends with *# but can't find the right regex for it.
Sample :
Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#
I am trying to find the string #*DCSN_RSN*# in the above string and replace it with <p>#*DCSN_RSN*#</p>
I think this is what you are looking for:
var sample = 'Hi this is test #*DCSN_RSN*# something found here #*DCSN_RerereSN.*#';
var replaced = sample.replace(/(#\*[\s\S]*?\*#)/g, '<p>$1</p>');
console.log(replaced);
To match any characters you can use [\s\S]*, then add ? for [\s\S]*? to make it less-greedy. Your * characters also need to be escaped.

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

RegEx: non-consecutive special characters only allowed in the middle [duplicate]

This question already has answers here:
Regex to find not start and end with dot and allow some special character only not all
(3 answers)
Closed 2 years ago.
I am using following
ng-pattern="/^[a-zA-Z][a-zA-Z0-9._](.*[a-zA-Z0-9])?$/"
The matching String should
not start with a special character,
not end with special character, and
not include consecutive symbols except . (dot) and _ (underscore).
But it is not working.
Please, any suggestion.
Try using the word character class as a start ([\w] = [a-zA-Z0-9_]):
I'm not sure what you mean by consecutive symbols. But this might help:
/^[a-zA-Z]([\w.]*[a-zA-Z0-9])?$/
Maybe, have a look at the JavaScript RegExp Reference

Categories

Resources