jQuery Data Type How to - Substring of varying length after a specific character








Question

We would like to know how to substring of varying length after a specific character.

Answer


<!DOCTYPE html>
<html>
<head>
<script type='text/javascript'>
function getByCategory(data, category) {
    return data.split(',').map(function(x) { 
        return x.trim();<!--from  ww  w  .  j  a v  a  2s.c  o m-->
    }).filter(function(y) {
        return y.indexOf(category) === 0;
    }).shift()
    .replace(category + '_', '');
}

document.writeln(
    getByCategory('A_1234_5678, B_2345_678, C_3456_78', 'B')
);

</script>
</head>
<body>
</body>
</html>

The code above is rendered as follows: