1

I have a string that comes to me like this: "[value1][value2]"

How can I get the values that are inside the square brackets? NOTE: if the string is like this "[][value2]" the first bracket that has a space must return a "" to me...

I have been trying a lot of regex and split but none workd.

this is the last I tried:

var pattern = /[([^]]*)]/g; 
var res = pattern.exec(datos[0].title); 

Another one I tried is:

var res = datos[0].title.match(/^.*?[([^]]*)].*?[([^]]*)]/gm);

but none do what I need...

I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces)

Jeff
  • 6,895
  • 1
  • 15
  • 33
Hex
  • 404
  • 7
  • 21
  • What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just use [`split`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/split) and then trim off the closing bracket. – Harry Cutts Nov 14 '18 at 00:08
  • Well Jeff, I have seen many regex that helps, but theyreturn the bracket, this is the last I tried: var pattern = /\[([^\]]*)]/g; var res = pattern.exec(datos[0].title); Another one I tried is: var res = datos[0].title.match(/^.*?\[([^\]]*)\].*?\[([^\]]*)\]/gm); but none do what I need... – Hex Nov 14 '18 at 00:11
  • Put it on the post. Please look at https://stackoverflow.com/help/how-to-ask – Abana Clara Nov 14 '18 at 00:14
  • 2
    Possible duplicate of [Regular expression to extract text between square brackets](https://stackoverflow.com/questions/2403122/regular-expression-to-extract-text-between-square-brackets) – slider Nov 14 '18 at 00:16
  • Abana, I don't know why it is relevant to post code that I've tested and does not do what It is required, but updated my post. I'm trying to find a way "that does it all" a regex that gets anything inside the Square brackets (even white spaces) – Hex Nov 14 '18 at 00:18

5 Answers5

2

As @HarryCutts stated, you don't need regex:

var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
cybersam
  • 63,203
  • 6
  • 53
  • 76
0

You can try this regex and the brute force way to extract the contents.

var regex = /\[(.*?)\]/g;

var value = "[value1][value2][]";

var matches = value.match(regex);

var matchedValues = matches.map(match => {
 return match.replace("[", "").replace("]", "");
}).join(" ");

console.log(matchedValues.toString())
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
0

You could just do this:

var str = "['value1']['value2']";
var value1 = str.split("]")[0].split("[")[1];
var value2 = str.split("]")[1].split("[")[1];
console.log(str);
console.log(value1);
console.log(value2);
Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
0

You can easily expand it for more values.

const string = "[value1][value2]";

const removeBrackets = (stringWithBrackets) => {
  return stringWithBrackets.split("][").map(s => s = s.replace(/\[*\]*/g, ""));
};

const [value1, value2] = removeBrackets(string);

console.log(value1, value2);
ams
  • 449
  • 5
  • 10
0
const getItems = (fullItemsString) => {
    let items = fullItemsString.replace(/\[/g, "").split("]");
    items.pop()

    return items;
}

Using:

let items = getItems("[2][][34][1]");

result: [ '2', '', '34', '1' ]