0

I Have string random length

Ex:

 sadsadsadsad(323213)dfsssds

 sadsadsadsad(321)dfsssds

How can I find the values in brackets?.

Value random length

Thank for reading

7 Answers7

1

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'));
hong4rc
  • 3,999
  • 4
  • 21
  • 40
1

Try This:

var str =  'sadsadsadsad(321)dfss888s(120)ds';

str.match(/\(\d+\)/g).map ( value => { console.log( value.replace(/[)(]/g, '') ) } );
Ehsan
  • 12,655
  • 3
  • 25
  • 44
0

You can use lastIndexOf and substring

var text = "sadsadsadsad(323213)dfsssds";
var newText = text.substring(text.lastIndexOf("(") + 1, text.lastIndexOf(")"));

console.log(newText)
michaelitoh
  • 2,317
  • 14
  • 26
0

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)
brk
  • 48,835
  • 10
  • 56
  • 78
0

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");
 }
Yuri
  • 2,820
  • 4
  • 28
  • 40
  • Thank for help, adsdadsdas(123)adadadad(123431) I need find (123) and (123431) how to? – Lightrain125 Sep 10 '18 at 15:47
  • Please refer to this article: https://stackoverflow.com/questions/6323417/how-do-i-retrieve-all-matches-for-a-regular-expression-in-javascript – Yuri Sep 10 '18 at 15:50
0

Try by using the following code it will helps you if you have remove nested Parentheses example "a(bcdefghijkl(mno)p)q"

function removeParentheses(s) {
    let openPatterns = [];
    for(let i=0; i < s.length; i++){

        if(s[i] == '('){
            openPatterns.push(i);
        }

        if(s[i] == ')'){
            const lastIndexOpen = openPatterns.pop();
            const originalTxt = s.slice(lastIndexOpen ,i+1);
            //here you have the raw text      
            const rawText = s.slice(lastIndexOpen+1,i);
            //this code works to remove the parentesis 
            //s= s.replace(originalTxt, reverseTxt);
            //i-=2;
        }
    }
    return rawText;    
}
Abel Valdez
  • 2,368
  • 1
  • 16
  • 33
0

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);
Emeeus
  • 5,072
  • 2
  • 25
  • 37