Find text javascript

前端 未结 7 1175
醉话见心
醉话见心 2021-01-26 16:32

I Have string random length

Ex:

 sadsadsadsad(323213)dfsssds

 sadsadsadsad(321)dfsssds

How can I find the values in brackets?.

<
相关标签:
7条回答
  • 2021-01-26 16:56

    Try This:

    var str =  'sadsadsadsad(321)dfss888s(120)ds';
    
    str.match(/\(\d+\)/g).map ( value => { console.log( value.replace(/[)(]/g, '') ) } );

    0 讨论(0)
  • 2021-01-26 16:57

    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);

    0 讨论(0)
  • 2021-01-26 16:58

    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");
     }
    
    0 讨论(0)
  • 2021-01-26 17:01

    You can use lastIndexOf and substring

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

    0 讨论(0)
  • 2021-01-26 17:14

    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)

    0 讨论(0)
  • 2021-01-26 17:15

    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'));

    0 讨论(0)
提交回复
热议问题