How can I get the value from a string that has the values inside a square bracket []?
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)
javascript split
add a comment |
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)
javascript split
What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just usesplit
and then trim off the closing bracket.
– Harry Cutts
Nov 14 '18 at 0: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...
– Hexxul
Nov 14 '18 at 0:11
Put it on the post. Please look at stackoverflow.com/help/how-to-ask
– Abana Clara
Nov 14 '18 at 0:14
2
Possible duplicate of Regular expression to extract text between square brackets
– slider
Nov 14 '18 at 0: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)
– Hexxul
Nov 14 '18 at 0:18
add a comment |
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)
javascript split
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)
javascript split
javascript split
edited Nov 14 '18 at 0:19
Jeff
6,34911025
6,34911025
asked Nov 14 '18 at 0:01
HexxulHexxul
8013
8013
What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just usesplit
and then trim off the closing bracket.
– Harry Cutts
Nov 14 '18 at 0: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...
– Hexxul
Nov 14 '18 at 0:11
Put it on the post. Please look at stackoverflow.com/help/how-to-ask
– Abana Clara
Nov 14 '18 at 0:14
2
Possible duplicate of Regular expression to extract text between square brackets
– slider
Nov 14 '18 at 0: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)
– Hexxul
Nov 14 '18 at 0:18
add a comment |
What do you know about the values in the brackets? If you know that they won't contain square brackets, you could just usesplit
and then trim off the closing bracket.
– Harry Cutts
Nov 14 '18 at 0: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...
– Hexxul
Nov 14 '18 at 0:11
Put it on the post. Please look at stackoverflow.com/help/how-to-ask
– Abana Clara
Nov 14 '18 at 0:14
2
Possible duplicate of Regular expression to extract text between square brackets
– slider
Nov 14 '18 at 0: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)
– Hexxul
Nov 14 '18 at 0:18
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
and then trim off the closing bracket.– Harry Cutts
Nov 14 '18 at 0:08
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
and then trim off the closing bracket.– Harry Cutts
Nov 14 '18 at 0: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...
– Hexxul
Nov 14 '18 at 0:11
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...
– Hexxul
Nov 14 '18 at 0:11
Put it on the post. Please look at stackoverflow.com/help/how-to-ask
– Abana Clara
Nov 14 '18 at 0:14
Put it on the post. Please look at stackoverflow.com/help/how-to-ask
– Abana Clara
Nov 14 '18 at 0:14
2
2
Possible duplicate of Regular expression to extract text between square brackets
– slider
Nov 14 '18 at 0:16
Possible duplicate of Regular expression to extract text between square brackets
– slider
Nov 14 '18 at 0: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)
– Hexxul
Nov 14 '18 at 0:18
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)
– Hexxul
Nov 14 '18 at 0:18
add a comment |
5 Answers
5
active
oldest
votes
As @HarryCutts stated, you don't need regex:
var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
add a comment |
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())
add a comment |
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);
add a comment |
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);
add a comment |
const getItems = (fullItemsString) =>
let items = fullItemsString.replace(/[/g, "").split("]");
items.pop()
return items;
Using:
let items = getItems("[2][34][1]");
result: [ '2', '', '34', '1' ]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291242%2fhow-can-i-get-the-value-from-a-string-that-has-the-values-inside-a-square-bracke%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
5 Answers
5
active
oldest
votes
5 Answers
5
active
oldest
votes
active
oldest
votes
active
oldest
votes
As @HarryCutts stated, you don't need regex:
var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
add a comment |
As @HarryCutts stated, you don't need regex:
var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
add a comment |
As @HarryCutts stated, you don't need regex:
var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
As @HarryCutts stated, you don't need regex:
var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
var x = "[value1][value2]";
console.log( x.slice(1,-1).split('][') );
answered Nov 14 '18 at 0:20
cybersamcybersam
39.8k43151
39.8k43151
add a comment |
add a comment |
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())
add a comment |
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())
add a comment |
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())
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())
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())
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())
answered Nov 14 '18 at 0:20
Sushanth --Sushanth --
49.9k64784
49.9k64784
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
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);
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);
answered Nov 14 '18 at 0:21
Jack BashfordJack Bashford
9,62131540
9,62131540
add a comment |
add a comment |
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);
add a comment |
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);
add a comment |
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);
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);
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);
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);
answered Nov 14 '18 at 0:51
amsams
2187
2187
add a comment |
add a comment |
const getItems = (fullItemsString) =>
let items = fullItemsString.replace(/[/g, "").split("]");
items.pop()
return items;
Using:
let items = getItems("[2][34][1]");
result: [ '2', '', '34', '1' ]
add a comment |
const getItems = (fullItemsString) =>
let items = fullItemsString.replace(/[/g, "").split("]");
items.pop()
return items;
Using:
let items = getItems("[2][34][1]");
result: [ '2', '', '34', '1' ]
add a comment |
const getItems = (fullItemsString) =>
let items = fullItemsString.replace(/[/g, "").split("]");
items.pop()
return items;
Using:
let items = getItems("[2][34][1]");
result: [ '2', '', '34', '1' ]
const getItems = (fullItemsString) =>
let items = fullItemsString.replace(/[/g, "").split("]");
items.pop()
return items;
Using:
let items = getItems("[2][34][1]");
result: [ '2', '', '34', '1' ]
answered Nov 14 '18 at 1:01
Fernando MatosFernando Matos
111
111
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53291242%2fhow-can-i-get-the-value-from-a-string-that-has-the-values-inside-a-square-bracke%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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
and then trim off the closing bracket.– Harry Cutts
Nov 14 '18 at 0: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...
– Hexxul
Nov 14 '18 at 0:11
Put it on the post. Please look at stackoverflow.com/help/how-to-ask
– Abana Clara
Nov 14 '18 at 0:14
2
Possible duplicate of Regular expression to extract text between square brackets
– slider
Nov 14 '18 at 0: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)
– Hexxul
Nov 14 '18 at 0:18