Reputation: 2019
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.
Upvotes: 0
Views: 705
Reputation: 174696
You could use the below regex to get the values inside []
braces,
\[([^\]]*)\]
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 *
,
\[(.*?)\]
Upvotes: 0
Reputation: 145378
Remove the quotes to make the argument a real regular expression literal:
// -------------------v --------v
var out = value.match(/\[(.*)\]/i);
Upvotes: 1