Regex to detect JavaScript Regex - javascript

I'm trying to write a regex that would detect regex in a javascript file. I've spent some time and this is what I've come up with:
\/.*?\/[a-zA-Z]?
This works fine:
/^[^0-9][2,3]$/
/^[a-z]+( +[a-z]+){2,}$/g
However it fails for something like this:
/\/.*?\/[a-zA-Z]?/g
Just to clarify my goal, I'm going to be using this regex to detect regexes in JavaScript code and color code them.

You should also check whether the / is preceeded by a \ or not to make sure it is not being escaped:
(?<!\\)\/.*?(?<!\\)\/[a-zA-Z]?
DEMO

Related

How can I incorporate a wildcard or regex into a Selenium test?

I have a pretty simple Nightwatch test written in javascript and part of it is to verify that the URL of the page is correct. The URL contains a random string of numbers each time the page is resubmitted, this string of numbers will change. The rest of the URL is static and already accounted for.
I have been searching and reading and still have not found a working solution, but I can't imagine its all that rare or difficult of a problem to overcome. I'm pretty brand new at javascript so I may be overlooking something simple, but I could really use some help.
You can use regular expression to verify the URL pattern. Use \d+ to match any number of digits.
For example if URL you're trying to match is www.example.com/path/123 where 123 is dynamic and can change you can use the following regex to test it.
var url = "www.example.com/path/123";
var regex = /www\.example\.com\/path\/\d+/;
regex.test(url)
Here regex.test(url) will return true for URL shown in example and also for URL's like www.example.com/path/111 or www.example.com/path/4 etc.
You can further improve the regex to ensure that it start with www by using ^ at the beginning of the regex like /^www\.example\.com\/path\/\d+/
Read more on regular expressions here
EDIT:
Here is a link to a JSFiddle I just wrote with the same code, that read's URL given in input field and test's its pattern with regex and shows alert with true or false based on result.
In your case I am guessing you are using selenium to read the current URL instead of reading it from an input. And you'll have to change the regex according to URL you are testing.
You can provide the URL you are testing in the input field and modify regex in the JSFiddle for testing.

JavaScript / Using system generated regex for validation

we want to use regex to validate a document structure. For this we simplify the document and the regex. The regex is generated out of a schema which is used for the validation.
The application is completly client based and coded in JavaScript.
A simple example is this regex:
regex1 = new RegExp(/~(A{1}B?C?(D*|E*|F*|G*)+){1}~/g)
That means the document structure can have this structure
A
-B
-D
-D
-D
-D
-D
So the document structure is parsed to ~ABDDDDD~
Now I want to validate if I can add "A" to the end which would result in this string: ~ABDDDDDA~
This does not match with the reg ex anymore:
"~ABDDDDDA~".match(regex1)
This does work quiet fine, but the document structure can grow and be like this: ~ABDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD~
A matching value can be matched quiet fast, but if the value is then:
~ABDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDA~
It takes very long, most times I just close the browser and reopen it.
Does anyone have ideas how to solve it?
Thanks!
UPDATE
The RegEx should also cover more, the structure can be quiet dynamic. I have not used a RegEx Generator, this example is parsed from a self developed script and is just an example.
It is in this case, that there is one root element A, then optional B or C. And now in a not given order any amount of D,E,F,G. But at least one!
So it should be valid for:
"~ABDDDDDFEG~"
"~AGGGGGEGGD~"
"~ABCDEFG~"
"~ABCDDDDDDDDDDDDDDDEFGGGGGG~"
Additionally it is possible, that that the E is limited to 0-5 occurances.
As soon as I work with the match either(A | B), there are real performance issues in all browsers. (IE, Chrome, Firefox)
Any ideas? Are there any alternatives to "match either(A | B)" with better performance?
The resulting regex should be as close as possible to:
~AB?C?[DEFG]*A?~
There are a lot of simplifications to do in your regex generator to get rid of the following points:
{1}: is literally useless, you can remove it from everywhere
(A*|B*)+: is strictly equivalent to [AB]*
Here is a Regex101: https://regex101.com/r/Lc6Fx8/1
Also, if you want help fixing your Regex generator, you should post some info about it.

Convert php regex to javascript exclude first 150 characters

I don't want to capture the first 150 characters and then capture everything else.
/(?<=\A.{150})(.*)/g
this is the expression I wrote in PHP but I need to do it in JavaScript and I cant work around not having lookbehind
this is javascript regex that I wrote
/(?:.*)(?!.{150})/g
You're overthinking it.
The pattern you want is /^.{150}(.*)/, and then you pull out the contents of capture group 1. And, for added fun, this pattern will work equally well in just about every engine under the sun, because it uses only basic token types and quantifiers.
Demo on Regex101
You don't need lookarounds for this
^.{150}(.+)
is all you need. https://regex101.com/r/P7keW9/1

Why does my regular expression work in PHP but not JavaScript?

I have a regex created by myself that I am currently running in PHP. Although when I merge it over to JavaScript, it refuses to work. I have also tried it in Python and it works perfectly fine.
Regex:
#[[](.[^]]+)[]][()](\d+)[)]
Testing in PHP, and working
Testing in JavaScript, and not working
JavaScript doesn't automatically escape your ].
This will help you get a visual idea:
PCRE:
JS:
Python:
So to fix this, you need to escape the brackets
#[[](.[^\]]+)[\]][()](\d+)[)]
// ^ ^
The best way to write this regex is to minimize the use of character classes:
#\[(.[^\]]+)\][()](\d+)\)
That's why it's good practice to escape this stuff instead of relying on quirks of the flavor.
I generated these images through regex101.

Java Regex replace function not working as intended

I need some help with a JS Regex.
Here's the string I'm passing, I want to delete everything before 'Hanyuu-sama' with JS Replace.
Hanyuu","dj":{"id":18,"djname":"Hanyuu-sama
The first and second "Hanyuu" can change, the id number can change. This has already been cropped quite a bit with regular expressions.
Now I've tried a few and surprisingly it's failing when I do simple and complex regexes:
I've tried:
.*\"
And it does nothing, I've tried disgusting stuff in my desperation:
.*\","dj\":{\"id":.*,\"djname\":\"
And nada.
Here's a JS Fiddle and here's a http://regex101.com/r/tE2uY0/1 Regex JS matching platform.
Does anyone know why this isn't working?
I know this is likely bad practice, I'm just trying to learn Regexes.
Bonus points if anyone can refer me to a good source to learn Regular expressions. I'd love a solution but I'd like to learn how to do this myself in the future and why this one failed even more.
Your method call should look like this:
source = source.replace(/.*"/, "");
Regular expression in javascript are written between /.../ and not "/.../" like they are in many other languages.
If your string is always structured like that and it does not contain any more characters, your regex should do the trick. That's because the * quantifier acts greedy by default, thus always matching the last " in the string.

Categories

Resources