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/
Related
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).
I write a little function to inject pieces of templates to the HTML without the use of "big" templating engines like EJS, Mustache.js, Nunjucks, Pure.js, etc.
I pass this piece of code with a key-value object:
<div><b>#{firstname} #{lastname}</b>, #{gender}</div>
Now I need to remove the extra spaces if someone try to write "#{ firstname }" in place of "#{firstname}".
I'm sorry but I don't know how to write the right ReGex to remove the spaces. How can I remove the spaces?
Thanks for your support.
UPDATE:
Thanks to #Saleem for the answer. Here a demo with the fix:
https://jsfiddle.net/avq3sntq/
Try following regex:
result = subject.replace(/(#\{)\s*(\S+)\s*(?=})/img, "$1$2");
If you input string is:
<div><b>#{ firstname } #{lastname}</b>, #{gender}</div>
Output will be:
<div><b>#{firstname} #{lastname}</b>, #{gender}</div>
As you can see, removes all extra spaces round firstname inside curly braces. See Demo here
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.
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)
I'm looking for a javascript regex that will remove all content wrapped in quotes(and the qoutes too), in a string that is the outlook format for listing email addresses. Take a look at the sample below, I am a regex tard and really need some help with this one, any help/resources would be appreciated!
"Bill'sRestauraunt"BillsRestauraunt#comcast.net,"Rob&Julie"robjules#ntelos.net,"Foo&Bar"foobar#cstone.net
Assuming no nested quotes:
mystring.replace(/"[^"]*"/g, '')
Try this regular expression:
/(?:"(?:[^"\\]+|\\(?:\\\\)*.)*"|'(?:[^'\\]+|\\(?:\\\\)*.)*')/g
Here's a regex I use to find and decompose the quoted strings within a paragraph. It also isolates several attendant tokens, especially adjacent whitespace. You can string together whichever parts you want.
var re = new RegExp(/([^\s\(]?)"(\s*)([^\\]*?(\\.[^\\]*)*)(\s*)("|\n\n)([^\s\)\.\,;]?)/g);