I Have string random length
Ex:
sadsadsadsad(323213)dfsssds
sadsadsadsad(321)dfsssds
How can I find the values in brackets?.
<Try This:
var str = 'sadsadsadsad(321)dfss888s(120)ds';
str.match(/\(\d+\)/g).map ( value => { console.log( value.replace(/[)(]/g, '') ) } );
You could catch the group within ()
using replace()
, and passing it to a callback function that push it into an array.
var str = 'sadsadsadsad(323213)dfsssds(1234)dfsssds(4567)dfsssds(abcf)';
var arr = [];
str.replace(/\(([a-z0-9]*)\)+/gi, (a,b)=>arr.push(b));
console.log(arr);
var str = "sadsadsadsad(323213)dfsssds";
var val= str.match(/\((.*?)\)/);
if (val) {
console.log("found");
}
You can use Regex
var str = "sadsadsadsad(323213)dfsssds";
var val= str.match(/\((.*?)\)/);
if (val) {
console.log("found");
}
You can use lastIndexOf
and substring
var text = "sadsadsadsad(323213)dfsssds";
var newText = text.substring(text.lastIndexOf("(") + 1, text.lastIndexOf(")"));
console.log(newText)
You can use indexOf
and substring
let str = 'sadsadsadsad(321)dfsssds';
let getFirstIndex = str.indexOf('(');
let getSecondIndex = str.indexOf(')');
let subStr = str.substring(getFirstIndex + 1, getSecondIndex);
console.log(subStr)
Try regex /\(.*\)/
function getValue(s) {
var strs = s.match(/\(.*?\)/g);
if (strs == null) {
return '';
}
return strs.map(str=>str.replace(/[()]/g, ''));
//return strs.join(' ,').replace(/[()]/g, '')
}
console.log(getValue('sadsadsadsad(323213)dfsssds(abc)'));
console.log(getValue('sadsad(_d_)sadsad(321)dfsssds'));
console.log(getValue('sadsad(33)sadsad(321dfsssds'));