简体   繁体   中英

keep string between two words node js express javascript

I have a string like:

var str = put returns between paragraphs abc_start indent code by 4 spaces abc_end quote by placing > at start of line abc_start  to make links abc_end.

I'm displaying this string in my browser using:

res.send('/page/+result);

I want to filter out result such that only the content which starts at abc_start and end at abc_end remains. How do I do that in node.js?

For eg: output: abc_start indent code by 4 spaces abc_end abc_start to make links abc_end

I tried using : str.split('abc_start').pop().split('abc_end').shift();

But I'm not gettting desired output.Please help.

<script>
function myFunction() {
    var str = "abc_start indent code by 4 spaces abc_end";
    var n = str.indexOf("abc_start ");
    var m = str.indexOf("abc_end");
    var res = str.slice(n+9, m-1);//Since you already know the length of the string "abc_start" that you want out of the way
    document.getElementById("demo").innerHTML = res;
}
</script>

Below is code snippet to solve this scenario:

var str = 'xxxadasyyydsdsxxxadadyyy';
var start = 'xxx';
var end = 'yyy'
var res = [];
find();
function find() {
    var initialIndex = str.indexOf(start);
    var lastIndex = str.indexOf(end);
    if(initialIndex > -1 && lastIndex > -1 && initialIndex < lastIndex) {
        res.push(str.substring(initialIndex, lastIndex + start.length));
        str = str.substring(lastIndex + start.length);
        find();
    } else {
        return;
    } 
}
console.log(res);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM