JS Regex on Curly Braces - javascript

I have an example statement:
"function(){var x=0;if(true){var y=0;}}"
I have tried many expressions but they only return the "{var y=0;}"
I want to extract the following result:
["{var x=0;if(true){var y=0;}}","{var y=0;}"]
What is the best possible regex for this?
I only use JavaScript so lookbehinds are not possible.

I just tested the following with a JS Regex tester and it seems to work:
Pattern - ({.*((?={).*(?:})).*})
Test String - "function(){var x=0;if(true){var y=0;}}"
Regex Tool - http://www.regextester.com/index2.html
This Regex works only on 2 nested curly brackets, may not work with 3 or more but the Regex can be adjusted accordingly.
Kind regards,
Yaron Shahrabani.

Related

Regex expression for accepsts only ()+ and numbers

I need a regular expression to use with Javascript .test
like using this examples
+999904321493214032
and
(+999)432143214
and
432143124321
Can some one please help me with this?
The tightest I can think of is:
^(?:\(\+\d+\)|\+)?\d+$
See live demo showing matching your examples and not matching input with the correct characters but that are malformed (eg imbalanced brackets).

Get all strings between two curly braces using regex in javascript

I need to get an array of all strings that are contained in curly brackets using JavaScript.
{{text is here}}
note that the text could contain all special characters and could be multi line i have tried this so far
regex test
\{{(.*?)\}}
In your demo you enabled m flag which is a wrong flag here. You need s flag or even without flags:
{{([^]*?)}}
Note: You don't need to escape braces here.
Live demo
Try the following:
(?<=\{{)(.*?)(?=\}})
it works for
{{text is here}}
https://regex101.com/r/gYXSbO/7/

looking for correct regular expression for pattern match in javascript

I tried searching stackoverflow for various combination but some or other thing stops working.
I am new to REGEX.
My input is <abc.1.1.1 or abc.1.1 or abc.1 --> case insensitive digits can be between 1-9 positive
var pattern= /[a-zA-Z].[1-9].[1-9].[1-9]$/;
the above pattern still accepts abc.a1.b.1
I am trying for following patterns abc.1.1.1orabc.1.1orabc.1
Any help would be appreciated
You should use literal . as just . means "any character" Also you could improve it a bit using global and ignoreCase flags. Also use anchors ^ and $
var pattern= /^[a-z]+\.(([1-9]\.))+[1-9]$/ig;
DEMO
This pattern will work
var pattern = /[a-z]*\.([0-9]\.?){,3}/i;
try this code
[a-z]+\.((([1-9]\.))*[1-9]+)*

convert js regex into python regex

I'm working on a part of a project, which is repleacing http url's with https url's if possible.
The Problem is, that the regular expressions for that are written for the javascript regex parser, but I'm using that regex inside python. To be compatible, I would rewrite the regex during parsing into a valide python regex.
as example, I have that regular expression given:
https://$1wikimediafoundation.org/
and I would a regular expression like that:
https://\1wikimediafoundation.org/
my problem is that I doesn't know how to do that (converting $ into \)
This code doesn't work:
'https://$1wikimediafoundation.org/'.replace('$', '\')
generate the following error:
SyntaxError: EOL while scanning string literal
This code work without error:
'https://$1wikimediafoundation.org/'.replace('$', '\\')
but generate a wrong output:
'https://\\1wikimediafoundation.org/'
Actually it works:
>>> 'https://$1wikimediafoundation.org/'.replace('$', '\\')
'https://\\1wikimediafoundation.org/'
>>> print 'https://$1wikimediafoundation.org/'.replace('$', '\\')
https://\1wikimediafoundation.org/
when you are doing 'https://$1wikimediafoundation.org/'.replace('$', '\\'), it's returning the __repr__ (~representation) of the string and you can see special characters.
By printing it, you are using the __str__, the readable version. (See this answer on __str__ vs __repr__)
try this:
'https://$1wikimediafoundation.org/'.replace('$', r'\')
adding r"\" whill automatically escape the backslash which you are trying to do.
You test your regex here https://regex101.com/, and then change it to python.
Additionaly, to replace the matched group, you can use re.sub module on these lines:
re.sub(r"'([^']*)'", r'{\1}', col ) )
replace
'Protein_Expectation_Value_Log(e)', 'Protein_Intensity_Log(I)'
{Protein_Expectation_Value_Log(e)}, {Protein_Intensity_Log(I)}
More you can refer here
Note that $& in replacement patterns should be converted to \g<0>, since \0 is \0x00 character in python regex

Regex to match part of a string

Regex fun again...
Take for example http://something.com/en/page
I want to test for an exact match on /en/ including the forward slashes, otherwise it could match 'en' from other parts of the string.
I'm sure this is easy, for someone other than me!
EDIT:
I'm using it for a string.match() in javascript
Well it really depends on what programming language will be executing the regex, but the actual regex is simply
/en/
For .Net the following code works properly:
string url = "http://something.com/en/page";
bool MatchFound = Regex.Match(url, "/en/").Success;
Here is the JavaScript version:
var url = 'http://something.com/en/page';
if (url.match(/\/en\//)) {
alert('match found');
}
else {
alert('no match');
}
DUH
Thank you to Welbog and Chris Ballance to making what should have been the most obvious point. This does not require Regular Expressions to solve. It simply is a contains statement. Regex should only be used where it is needed and that should have been my first consideration and not the last.
If you're trying to match /en/ specifically, you don't need a regular expression at all. Just use your language's equivalent of contains to test for that substring.
If you're trying to match any two-letter part of the URL between two slashes, you need an expression like this:
/../
If you want to capture the two-letter code, enclose the periods in parentheses:
/(..)/
Depending on your language, you may need to escape the slashes:
\/..\/
\/(..)\/
And if you want to make sure you match letters instead of any character (including numbers and symbols), you might want to use an expression like this instead:
/[a-z]{2}/
Which will be recognized by most regex variations.
Again, you can escape the slashes and add a capturing group this way:
\/([a-z]{2})\/
And if you don't need to escape them:
/([a-z]{2})/
This expression will match any string in the form /xy/ where x and y are letters. So it will match /en/, /fr/, /de/, etc.
In JavaScript, you'll need the escaped version: \/([a-z]{2})\/.
You may need to escape the forward-slashes...
/\/en\//
Any reason /en/ would not work?
/\/en\// or perhaps /http\w*:\/\/[^\/]*\/en\//
You don't need a regex for this:
location.pathname.substr(0, 4) === "/en/"
Of course, if you insist on using a regex, use this:
/^\/en\//.test(location.pathname)

Categories