From this string:
dfasd {{test}} asdhfj {{te{st2}} asdfasd {{te}st3}}
I would like to get the following substrings:
test, te{st2, te}st3
In other words I want keep everything inside double curly braces including single curly braces.
I can't use this pattern:
{{(.*)}}
because it matches the whole thing between first {{ and last }}:
test}} asdhfj {{te{st2}} asdfasd {{te}st3
I managed to get the first two with this regex pattern:
{{([^}]*)}}
Is there any way to get all three using regex?
Try {{(.*?)}}.
.*? means to do a lazy / non greedy search => as soon as }} matches, it will capture the found text and stop looking. Otherwise it will do a greedy search and therefore start with the first {{ and end with the very last }}.
This isn't that pretty, but it doesn't use RegEx and makes it clear what you are trying to accomplish.
const testString = 'dfasd {{test}} asdhfj {{te{st2}} asdfasd {{te}st3}}';
const getInsideDoubleCurly = (str) => str.split('{{')
.filter(val => val.includes('}}'))
.map(val => val.substring(0, val.indexOf('}}')));
console.log(getInsideDoubleCurly(testString));
Related
I need to select the values that are in a string between double parentheses and double-braced but that allow non-double braces and parentheses
I used the following expression for the double-braced but it is broken if it has a braced inside the string. this should only be broken by having double-braced, but I do not know how to make the regular expression
/{{([^}]*)}}/g
and
/\({2}([^)]*)\){2}/g
I tried adding double-braced here, but it does not work:
/{{([^}}]*)}}/g
Because you want to permit single braces inside, you shouldn't use a negative character set - instead, start at the left delimiter and lazy-repeat any character until you get to the right delimiter. For example:
/{{(.*?)}}/
const pattern = /{{(.*?)}}/g;
const str = 'foo{{bar}} foo{{baz}} foo{{with}bracket}}';
console.log(str.match(pattern));
I think you're missing the backslash in your parentheses.
Maybe something like /\{\{([^)]+)\}\}/ would work
Example:
console.log("{{TEST}}".match(/\{\{([^)]+)\}\}/)[1]);
console.log("{{TE{{}}ST}}".match(/\{\{([^)]+)\}\}/)[1]);
Hope that helps
what #certainperformance said is correct.
I just want to add a expression for both parentheses and braces as you asked for in question.
**You can try this for both parentheses and braces **
((?:{{)(.*?}}))|((?:\(\()(.*?\)\)))
Demo
const regex = /((?:{{)(.*?}}))|((?:\(\()(.*?\)\)))/gm;
const str = `foo{{bar}} foo{{baz}} foo{{with}bracket}}
foo((hello))
test{{test))
{{test))
{{test}}
((test))
test))
`;
let m;
let op = str.match(regex);
console.log(op);
I have the following string:
const myString = '"{{ some text here }}"'
I want to remove the quotes before {{ and after }}. I can do:
myString.replace('"{{', '{{').replace('}}"', '}}')
But I'm wondering if there is a better way to achieve this without calling replace twice?
Does anyone can let me know if there is a better way and explain?
http://jsbin.com/xafovamihi/edit?js,console
You may use a regex with an alternation group where you can capture {{ and }} into Group 1 and 2 respectively and replace with the backreferences only:
var myString = '"{{ some text here }}"';
console.log(myString.replace(/"({{)|(}})"/g, '$1$2'))
Details:
"({{) - matches " and captures {{ into Group 1
| - or
(}})" - captures }} into Group 2 and matches "
Since the replacement backreferences, when referencing a group that did not participate in the match, are initialized with an empty string, this $1$2 replacement will work properly all the time.
The global modifier /g will ensure all matches are replaced.
An easy way would be to combine the two patterns into one match using regex OR, so you get /"{{|}}"/g
Why not to use regex to find {{ with }} and any text between?
const myString = '"{{ some text here }}"',
reg = /{{.*?}}/;
console.log(myString.match(reg)[0]);
I have a string like the following:
SOME TEXT (BI1) SOME MORE TEXT (BI17) SOME FINAL TEXT (BI1234)
Question
I am trying to make a regex to get just the information between the curly brackets, for example the end string would look like:
BI1 BI17 BI1234
I have found this example on stackoverflow which will get the first value BI1, but will ignore the rest after.
Get text between two rounded brackets
this is the REGEX I created from the above link: /\(([^)]+)\)/g but it includes the brackets, I want to remove these.
I am using this website to attempt to solve this query which has a testing window to see if the regex entered works:
http://www.regexr.com
Additional Information
there can be any amount of numbers also, which is why I have given 3 different examples.
this is a continous string, not on seperate lines
thanks for any help on this matter.
While this isn't possible using just regexes, you can do it with string#split and the following regex:
\).*?\(|^.*?\(|\).*?$
Yielding code that looks a bit like this:
function getBracketed(str) {
return str.split(/\).*?\(|^.*?\(|\).*?$/).filter(Boolean);
}
(You need to filter out the empty strings that'll appear at the beginning and end if you do it this way - hence the extra operation).
Regex demo on Regex101
Code demo on Repl.it
If you need to keep all inside parentheses and remove everything else, you might use
var str = "SOME TEXT (BI1) SOME MORE TEXT (BI17) SOME FINAL TEXT (BI1234)";
var result = str.replace(/.*?\(([^()]*)\)/g, " $1").trim();
console.log(result);
If you need to get only the BI+digits pattern inside parentheses, use
/.*?\((BI\d+)\)/g
Details:
.*? - match any 0+ chars other than linebreak symbols
\( - match a (
(BI\d+) - Group 1 capturing BI + 1 or more digits (\d+) (or [^()]* - zero or more chars other than ( and ))
\) - a closing ).
To get all the values as array (say, for later joining), use
var str = "SOME TEXT (BI1) SOME MORE TEXT (BI17) SOME FINAL TEXT (BI1234)";
var re = /\((BI\d+)\)/g;
var res =str.match(re).map(function(s) {return s.substring(1, s.length-1);})
console.log(res);
console.log(res.join(" "));
The string that i have is
[<span class="link" nr="28202" onclick="javascript:openAnlaggning('/kund/Bridge/Main.nsf/AnlOkatSamtligaFaltCopy/045C9DDFDC7AB308C1257C1B002E11F1?OpenDocument&urval=1');" >Alingsås Järnvägsstation</span>]
The logic is to check if there is a '[' at the start of the string and if it is present then take the value between the square brackets. In the above string what i would like to get as output is
<span class="link" nr="28202" onclick="javascript:openAnlaggning('/kund/Bridge/Main.nsf/AnlOkatSamtligaFaltCopy/045C9DDFDC7AB308C1257C1B002E11F1?OpenDocument&urval=1');" >Alingsås Järnvägsstation</span>
I tried with this
var out = value.match('/\[(.*)\]/i');
I tried it on scriptular.com,and i do get a match.
Thanks in advance.
Remove the quotes to make the argument a real regular expression literal:
// -------------------v --------v
var out = value.match(/\[(.*)\]/i);
You could use the below regex to get the values inside [] braces,
\[([^\]]*)\]
DEMO
Your regex \[(.*)\] will not work if there is another ] at the last. See the demo.
For this you have to make your regex to do a non-greedy match by adding ? quantifier next to *,
\[(.*?)\]
DEMO
Say I have the string Hello {{name}}, how are you doing today? I'm trying to grab name from that string.
So far, I have /\{{2}[a-z0-9]*\}{2}/gi. The problem, is, it grabs {{name}} and not name. Without the global flag it works fine, but I'm trying to get every instance of words within double brackets, so it's not quite right... I'm no RegEx pro so I'm hoping someone can help me out...
The best solution would be to use lookaround assertions so the {{ and }} don't get picked up, however JavaScript regex doesn't support lookbehind, it only supports lookahead.
So one alternative is to place your text in a capture group and grab what's inside:
/\{{2}([a-z0-9]*)\}{2}/gi
To get every capture, make a RegExp object with your regex, and iterate through the results of its exec() function. For example:
var str = 'Hello {{name}}, how are you doing {{date}}?';
var re = /\{{2}([a-z0-9]*)\}{2}/gi;
var words = [];
var match;
while (match = re.exec(str)) {
words.push(match[1]);
}
jsFiddle sample
Or as Gumbo suggests in his comment, manually strip out the {{ and }} from your array of matches.
Yea as mentioned, pattern matching on the 2 opening and closing braces is the way to go (assuming the name does not have curly braces in succession of the number of 2 within itself, either opening/closing)
/\{{2}([a-z0-9]+)*\}{2}/gi