regex match number in parentheses (Firefox) [duplicate] - javascript

This question already has answers here:
Javascript regex (negative) lookbehind not working in firefox
(4 answers)
Closed 4 years ago.
I have string like this:
(3) Request Inbox
Now I want to detect number 3 in parentheses. Pay attention just 3. I write this regex in javascript but it doesn't work in Firefox.
var regex = new RegExp(/(?<=\()\d+(?:\.\d+)?(?=\))/g);
Error in console: SyntaxError: invalid regexp group
Demo link

Positive lookbehind is not supported by most browsers so use an alternative way to get your answer.
Something like this,
var string = "somestring(12)";
var exp = /(?:\()(\d+)(?:\.\d+)?(?=\))/;
var mat = string.match(exp);
if(mat) {
console.log(mat[1]);// second index
}
This should only give 12 as the answer

For a full match try this:
var regex = new RegExp(/(?=\d+\))\d+/g);

Related

String starting with backslash hacks my regex [duplicate]

This question already has answers here:
How can I use backslashes (\) in a string?
(4 answers)
Closed 3 years ago.
My string should be in the IRC command format : "/add john".
So, i created this Regex :
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test('\/add user1');
alert(bool);
The problem is either I use /***/ or RegExp syntax, if I set a backslash at the beginning of my string (like in my example above), my alert pop up show "true" and I don't want that.
I code in Javascript
You can use String.raw to make sure that the backlash is not removed when testing your input:
var regex = /^\/add ([A-Za-z0-9]+)$/
var bool = regex.test(String.raw`\/add user1`);
alert(bool);
You can play with this code here: https://jsbin.com/ziqecux/25/edit?js

Why is this regex incorrect? [duplicate]

This question already has answers here:
Why do regex constructors need to be double escaped?
(5 answers)
Closed 4 years ago.
I have the following regular expression:
\d{4}(-\d{2}){2}
Which is supposed to match dates that follow the YYYY-MM-DD format, so 1990-01-01 should successfuly match. However this fails when I try it in javascript.
var x = new RegExp('\d{4}(-\d{2}){2}')
x.test('1990-02-01') //why is this false?
Use the regular js regex syntax. Like this:
var x = /\d{4}(-\d{2}){2}/;
console.log(x.test('1990-02-01'));
if you want to keep the new RegExp part, you have to escape the string's backslashes:
var x = new RegExp('\\d{4}(-\\d{2}){2}');
console.log(x.test('1990-02-01'));

Regex gives different result in javascript and .NET [duplicate]

This question already has an answer here:
Convert JavaScript Regex to C#
(1 answer)
Closed 4 years ago.
I need a regex which matches pattern name1\name2 with the restriction that name1 must not contain some special characters such as < , >. name1, name2 can have spaces.
I am using this regex and it seems to work fine in java script :
/^[^ &<>;]+\\./
In my C sharp code, I am using the regex below:
var pattern= #"^[^ &<>;]+\\.";
The C sharp results fail for the input : 8 [ } \ ;
where as it passes for javascript.
How can I get similar results ?
Problem with the example that you've given, you ommited space from list of allowed characters. For your example this pattern works:
var pattern = #"^[^&<>;]+\\.";

Python Regex to get string(extension) after special character [duplicate]

This question already has an answer here:
Extract until end of line after a special character: Python
(1 answer)
Closed 5 years ago.
I have a string want to get string after special character which is extension.
i tried and it is works fine in javascript but wasnt in python.. how to do that?
here is my JS snippet,
my_str_0 = ".exr[6,7]";
my_str_1 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%04d.exr[6] 1-7";
my_str_2 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%05d.png[1] 1-10";
my_str_3 = "/home/mohideen/test_dir/Samp_8860-fg_paint_%05d.jpg";
for (var i in [my_str_0, my_str_1, my_str_2, my_str_3]) {
var reg = /([^.]*)$/.exec(eval("my_str_"+i));
console.log(reg[0]);
}
here is my python regex link
Use MultiLine Flag.
See here in this link

Javascript regex in attribute [duplicate]

This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I need to use data attribute for regex just as
<div data-regex="REGEX-HERE">
and then get the value by javascript and put in a variable. and then do a test like
var regex = $(this).attr("data-regex");
regex.test(name)
when I tried to use "^[\x20-\x7E]+$" for testing english character is didn't work.
Note when I tried this
var regex = /^[\x20-\x7E]+$/;
It worked.
Thanks in advance
You can do this:
var regex = new RegExp("^[\x20-\x7E]+$",""); // Modifiers on the tend
So finally:
var regex = new RegExp($(this).data("regex"));
regex.test(name)

Categories

Resources