JS regular expression to match imdb url - javascript

Can any one tell me what is wrong with this javascript code
"http://www.imdb.com/title/tt2618986/".match("~http://(?:.*\.|.*)imdb.com/(?:t|T)itle(?:\?|/)(..\d+)~i");
When i try this here https://regex101.com/r/yT7bG4/1 it works but not in javascript

The way you create a regular expression in JavaScript is /pattern/flags. The code you are looking for is something along the lines of:
"http://www.imdb.com/title/tt2618986/".match(/http:\/\/(?:.*\.|.*)imdb.com\/(?:t|T)itle(?:\?|\/)(..\d+)/i);
You have to escape all of the / in the regular expression so the / become part of the regular expression instead of indicating the end of it. I would suggest reading this article if you want to learn more about regular expressions in JavaScript.
Also, https://regex101.com/ has a JavaScript option on the left, under the 'FLAVOR' banner, which may help knowing which flags are valid.

You are using pcre(php) flavor in regex101. You should select javascript flavor.
Considers that there is not '~' delimiter in javascript RegExp. This is why your code is not working.
You should write something like:
"http://www.imdb.com/title/tt2618986/".match(/http:\/\/(?:.*\.|.*)\.imdb.com\/(?:t|T)itle(?:\?|\/)(..\d+)/i);

In your case:
/ symbol must be escapes - like this /.
there is not '~' delimiter
Result code with regular expression is:
"http://www.imdb.com/title/tt2618986/".match(/http:\/\/(?:.*\.|.*)imdb.com\/title(?:\?|\/)(..\d+)/i)
p.s. use modifier 'i' to do a case-insensitive search

Related

Regex special character '{' matches in JS but not in Java

test string: abc{123
regex: \w+{\d+
This matches in JS, but when I try to match it in Java it gives me this error:
Illegal repetition near index 2
\w+{\d+
It works in Java only when I escape the { character like this: \w+\{\d+
I tried it on these two links:
JS Link : http://myregexp.com/index.html
Java Link:http://www.ocpsoft.org/tutorials/regular-expressions/java-visual-regex-tester/
Desired result: If it matches in JS, it should match in Java also.
What is the difference between the regex implementation in Java and JS? How can I make it behave in the same way in Java and in JS?
How can I make it behave in the same way in Java and in JS?
You already know the answer:
It works in Java only when I escape the { character like this: \w+\{\d+".
Why? Because JavaScript here is a bit more permissive. Note that in JavaScript \w{3 will match "f{3", but not "f77"; \w{3} will match "f77" but not "f{3}". That is to say, the same character { changes meaning based on whether or not somewhere later in the string an } appears. The behaviour is thus made more unpredictable by its permissiveness, and Java just does not allow you to write regular expressions so sloppily.
you have to escape special characters and since a backslash is also a special character, you have to escape it as well. the regex will look like this in java: \\w+\\{\\d+. if you have problems, feel free to ask. you can generate a code in several programming languages here: https://regex101.com/r/D4yz40/1 this example matches your string. you can then generate the code for java and js
You just need to escape the {. So the regex should look like this:
\w+\{\d+
Your initial regex isn't valid.. Javascript is just more forgiving in this case.. But { is one of the characters you want to escape in regex since it means how many times to repeat a specific character(s) like so: [a-z]{22} would match 22 sequential characters from a-z..

JavaScript regular expression to have unique contents

I want a regular expression which allows
__$1__, __$2__, ... __$9__
or
__$an alphanumeric word up to 6 characters__
in a string...
I have tried with below expression but it's not working as required:
/^.*(\_\_\\$[1-9]{1}\_\_|\_\_\\$[a-zA-Z0-9]{0,6}\_\_)\1{1}.*$/;
Also, there should not be any repeated $ content.
I'd go with:
/__\$([0-9]|[A-z0-9]{1,6})__/
This should fit your requirements except for:
Also, there should not be any repeated $ content.
I guess this can't be accomplished using just Regular Expressions, at least as far as I know...
How about this?
/__\$([0-9]|[A-z0-9]{1,6})__/
or
/__\$([0-9]|[A-z]{1}[A-z0-9]{0,5})__/

What does this Javascript statement (regular expression) mean?

What does the RegEx test for here?
function chksql(){
if (/^\s*(?:delete|drop|truncate|alter)/.test(v)) return false;
}
I just know it's mixed with regular expression, but can't figure out what it means.
it means its checking if v is a string that starts with zero or more white space charcters followed by delete or drop or truncate or alter
so if v were " alter" this would return false.
see docs: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
I should add that checking against this happening on the client side is a really bad idea. It will be circumvented.
There are a number of good online tools for testing and exploring regular expressions these days.
One I like is debuggex.com. Here's what it displays for your regular expression:
^\s*(?:delete|drop|truncate|alter)
Debuggex Demo
To interpret that, you still need to do a bit of homework like finding out what ^ and \s mean, but the "railroad diagram" helps show what the regular expression is testing for. Just follow the lines to see what it will match. You can also try typing in test strings at the link above to see how it matches (or doesn't match) them.
Another good site is regex101.com. Here's your regular expression there. They give you an English description of what the regular expression is looking for.
Also, heed mkoryak's advice about trying to sanitize SQL on the client!

How to replace a substring with open parentheses (

I am a Regex newbie and trying to implement Regex to replace a matching pattern in a string only when it has a ( - open parentheses using Javascript. for example if I have a string
IN(INTERM_LEVEL_IN + (int)X_ID)
I would only like to highlight the first IN( in the string. Not the INTERM_LEVEL_IN (2 ins here) and the int.
What is the Regex to accomplish this?
To match the opening bracket you just need to escape it: IN\(.
For instance, running this in Firebug console:
enter code here"IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN()/, 'test');`
Will result in:
>>> "IN(INTERM_LEVEL_IN + (int)X_ID)".replace(/(IN\()/, 'test');
"testINTERM_LEVEL_IN + (int)X_ID)"
Parenthesis in regular expressions have a special meaning (sub-capture groups), so when you want them to be interpreted literally you have to escape them by with a \ before them. The regular expression IN\( would match the string IN(.
The following should only match IN( at the beginning of a line:
/^IN\(/
The following would match IN( that is not preceded by any alphanumeric character or underscore:
/[a-zA-Z0-9_]IN\(/
And finally, the following would match any instance of IN( no matter what precedes it:
/IN\(/
So, take your pick. If you're interested in learning more about regex, here's a good tutorial: http://www.regular-expressions.info/tutorial.html
You can use just regular old Javascript for regex, a simple IN\( would work for the example you gave (see here), but I suspect your situation is more complicated than that. In which case, you need to define exactly what you are trying to match and what you don't want to match.

Get Part of String

I am not good at Regular expression and couldn't find an easy way for this problem.
i have an expression like:
TR_NN_Expression
Where NN is a number of 2 digits, and Expression can contain '_', so i can't use split for this. I would like to get the Expression. Any help would be greater appreciated.
You can use this regular expression:
TR_[0-9]{2}_(.*)
The part you want will be in the capturing group. Example usage:
> s = 'TR_01_My##34_Expresion'
"TR_01_My##34_Expresion"
> s.match(/TR_[0-9]{2}_(.*)/)[1]
"My##34_Expresion"
I always use and recommend this tool, It makes our life to easier,
Interactive multi-language regular expression generator
Enjoy!
If the prefix is of fixed length and you know that the strings are of the correct format you can just use substring to accomplish this.
"TR_42_some_expression_here".substring(6) // yields "some_expression_here"
If you have a more complicated situation, regular expressions may be appropriate. The exact expression depends on what you wish to capture.

Categories

Resources