You can use a regular expression to retrieve those substrings.
The problem is that JS doesn't have lookbehinds. Then, you can retrieve the text with the brackets, and then remove them manually:
(document.getElementById('mytext').textContent
.match(/\[.+?\]/g) // Use regex to get matches
|| [] // Use empty array if there are no matches
).map(function(str) { // Iterate matches
return str.slice(1,-1) // Remove the brackets
});
Alternatively, you can use a capturing group, but then you must call exec
iteratively (instead of a single match
):
var str = document.getElementById('mytext').textContent,
rg = /\[(.+?)\]/g,
match;
while(match = rg.exec(str)) // Iterate matches
match[1]; // Do something with it