0

I have a string as follows

var str = "foobar~~some example text~~this is a string, foobar1~~some example 
text1~~this is a string1";

I need to loop through this string and get the text "some example text", "some example text1"

Can any one let me know how to loop through this.

3
  • Please a) format the question b) show what you've tried Commented Sep 2, 2017 at 11:03
  • You want to check if the search text is present or you want its position in string? Commented Sep 2, 2017 at 11:06
  • @LouysPatriceBessette, any example please... Commented Sep 2, 2017 at 11:07

4 Answers 4

2

You may use .match() with the following regex:

/~~[^~]+~~/g

In order to loop you may use .forEach() on resulting array:

reatVal.forEach(function(ele, idx) {
    console.log('element n.: ' + idx + ' value: ' + ele)
})

var str = "foobar~~some example text~~this is a string, foobar1~~some example text1~~this is a string1";
var retVal = str.match(/~~[^~]+~~/g).map(function(ele, idx) {
    return ele.replace(/~~/g, '');
});


console.log('retVal is the following array: ' + retVal);

retVal.forEach(function(ele, idx) {
    console.log('element n.: ' + idx + ' value: ' + ele)
})

Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend the use of capturing groups and a bit of helper methods:

String.prototype.getCapturingGroups = function(re){
  if(re instanceof RegExp){
    let groups_contents = [];
    this.replace(re, function(str, match){
      groups_contents.push(match);
    });
    return groups_contents;
  }
  return [];
}

var str = "foobar~~some example text~~this is a string, foobar1~~some example text1~~this is a string1";
var regex = /\~\~([^~]+)\~\~/g;
var content_arr = str.getCapturingGroups(regex);

content_arr.forEach((e,i)=>console.log(`n°${i} is : ${e}`))

Comments

0

I tried this and got it done.

var arr = str.split(',');
    for (var i = 0; i < arr.length; i++) {
        var onetext = arr[i];
        var twotext = onetext.split('~~');
        for (var j =0; j< twotext.length; j++) {
            console.log(twotext[j]);
        }
    }

Thanks for all your quick responses.

Comments

0
    var str = "foobar~~some example text~~this is a string, 
foobar1~~some example text1~~this is a string1";
    var strArr = str.split(',');
    var finalStrArr = []; 
    for(var i=0; i<strArr.length; i++) { 
        var finalStr = strArr[i].split('~~'); 
        finalStrArr.push(finalStr[1]); 
    };

This code will work only if the string comes only in that format. I mean the above code will give you the 1st string after the characters ~~.
I put finalStrArr.push(finalStr[1]); I am getting the index of 1 because I am assuming that the expected string will always be in that position.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.