-4

I have a string:

 let str = ' some text url:"http:/myURL01", some text url:"http:/myURL02", ..., url:"http:/myURL03", some text';

Now, i want to get all URL (sub string between url:" and ", ) and add to an array

Expect result: arrURL ["myURL01", "myURL02", "myURL03"]

Thank you!

Eddie
  • 26,593
  • 6
  • 36
  • 58
  • 1
    Possible duplicate of [JavaScript text between double quotes](https://stackoverflow.com/questions/19793221/javascript-text-between-double-quotes) – Eddie Jun 13 '18 at 03:05
  • 1
    Have you [**tried anything**](http://meta.stackoverflow.com/questions/261592) so far? – Obsidian Age Jun 13 '18 at 03:07

4 Answers4

2

Try below

var str = ' some text url:"http:/myURL01", some text url:"http:/myURL02", ..., url:"http:/myURL03", some text';
    var arr = [];
 var x=0;
 do
 {
  x = str.indexOf("url:", x);
  if(x!=-1)
  { 
   arr.push(str.substring(x+5,  str.indexOf(",",x)-1))
   x = x+1;
  }
 }
 while(x!=-1);

  console.log(arr)
user1531038
  • 266
  • 1
  • 6
  • 1
    you should convert this into a [runnable js snippet](https://stackoverflow.blog/2014/09/16/introducing-runnable-javascript-css-and-html-code-snippets/). – kiddorails Jun 13 '18 at 03:26
2

let str = ' some text url:"http:/myURL01", some text url:"http:/myURL02", ..., url:"http:/myURL03", some text';
results = str.match(/http.*?(?=\")/g);
console.log(results);

Regex explanation - https://regex101.com/r/CMiWcr/1/

kiddorails
  • 12,961
  • 2
  • 32
  • 41
0

Just use split

    let str = ' some text url:"http:/myURL01", some text url:"http:/myURL02", ..., url:"http:/myURL03", some text';

    var arrURL = str.split(",");    
    var i;
    for (i = 0; i < arrURL .length; i++) {
        arrURL[i] = arrURL[i].split("url:").pop();
    }
  • 1
    `split` will not give the right answer. It will just break by comma and won't give the URLs. You have to use regex match. – kiddorails Jun 13 '18 at 03:12
0
$(document).ready(
    function() {
        var str = ' some text url:"http:/myURL01", some text 
    url:"http:/myURL02", ..., url:"http:/myURL03", some text';
        var trg = new Array();
        $('#elementId').click(function() {
            var list = str.split("http:/");
            for (var i = 1; i < list.length; i++) {
                var elementStr = list[i].split('"');
                trg[i - 1] = '"http:/'+elementStr[0]+'"';
            }
        });
    });

Where 'elementId' is an element id for triggering event

Ramesh
  • 76
  • 1
  • 9