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

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 = #"^[^&<>;]+\\.";

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

regex match number in parentheses (Firefox) [duplicate]

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

Javascript - Make specific text a link [duplicate]

This question already has answers here:
Detect URLs in text with JavaScript
(15 answers)
Closed 4 years ago.
I'm looking at a 'to do' list application that uses JavaScript.
One of the functions converts text in to a hyperlink when https, https or ftp is present.
I'd like to expand this so if my text contains # followed by any 5 digit number, that becomes a link as well. The URL should be http://192.168.0.1/localsite/testschool/search.php?id=ID
where ID is the # and 5 digit number.
This is the current Javascript:
function prepareHtml(s)
{
// make URLs clickable
s = s.replace(/(^|\s|>)(www\.([\w\#$%&~\/.\-\+;:=,\?\[\]#]+?))(,|\.|:|)?(?=\s|"|<|>|\"|<|>|$)/gi, '$1$2$4');
return s.replace(/(^|\s|>)((?:http|https|ftp):\/\/([\w\#$%&~\/.\-\+;:=,\?\[\]#]+?))(,|\.|:|)?(?=\s|"|<|>|\"|<|>|$)/ig, '$1$2$4');
};
called using prepareHtml(item.title)
Any idea how I can do this ?
I've worked out regex to match the # and 5 digits is ^#([0-9]{5}) but I'm not sure how to implement this in the function.
Thanks
Looks to me that the pattern & replace string can be simplified a bit.
function urls2links(s)
{
return s.replace(/(^|[\s>])(www\.)/gi, '$1http://$2')
.replace(/\b(((https?|ftps?):\/{2})(\S+?))(?="|<|>|[\s<>\"]|$)/ig, '$1');
};
var str = 'blah http://192.168.0.1/localsite/testschool/search.php?id=#12345 \nblah www.foo.com/bar/" blah';
console.log('--\n-- BEFORE\n--');
console.log(str);
console.log('--\n-- AFTER\n--');
console.log(urls2links(str));
Although I'm not too sure about needing to include the character entities |"|<|> in the lookahead.
But I'm guessing you also deal with encoded strings.

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

How do I properly use RegExp in JavaScript and PHP? [duplicate]

This question already has an answer here:
Learning Regular Expressions [closed]
(1 answer)
Closed 6 years ago.
How do I properly use RegExp?
var text = "here come dat boi o shit waddup";
var exmaple = /[a-zA-Z0-9 ]/; // allowes a-zA-Z0-9 and whitespaces but nothing else right?
example.test(test); // would return true right?
text = "%coconut$ยง=";
example.test(text); // would return false right?
//I know this is very basic - I started learnig all this about week ago
Are JS RegExp's the same as PHP RegExp's?
How do I define banned characters instead of defining allowed characters?
How do I make it so that the var text has to contain 3 (or more) numbers/letters?
How do I include / or ",'$ etc. in my pattern?
No.
Use ^ character (i.e. [^abc] will exclude a, b and c)
Use [A-Za-z]{3} for letters and \d{3} for digits. If you want 3 or more, use \d{3,}
Use escape character (\/, \', \", '\$')

Categories

Resources