skip to Main Content

For example I have strings which has multiple URLs.

var str = '"test.com","test2.com","test3.com/123,312","test4.com/123,312"'

I want to make array from this string.

at first I try to use split(",") however some URL has ",".

Is there any good idea to separate these?

5

Answers


  1. If the string is split up by "," (a quote, a comma, and a quote), then we have all the URL’s which can maintain their internal commas. Then we just have to remove any quotes that are left in each string of the array

    var str = '"test.com","test2.com","test3.com/123,312","test4.com/123,312"';
    var urls = str.split(`","`).map(s => s.replace(/"/g, ''));
    console.log(urls);
    Login or Signup to reply.
  2. Yes, you can employ a better approach to extract the URLs from the provided string. Since the URLs themselves might include commas, a basic comma-based split won’t be dependable. Instead, you can utilize regular expressions to locate the URLs within the string. Here’s an example:

    var str = '"test.com","test2.com","test3.com/123,312","test4.com/123,312"';
    
    // Match URLs within the string
    var urls = str.match(/"([^"]+)"/g);
    
    // Remove double quotes from matched URLs and create an array
    var urlArray = urls.map(url => url.replace(/"/g, ''));
    
    console.log(urlArray); 
    

    This code uses a regular expression /"([^"]+)"/g to match strings enclosed in double quotes. The match function returns an array of matched strings. Then, the map function is used to remove the double quotes from each matched URL, creating the final array of URLs.

    Login or Signup to reply.
  3. Maybe just pull the url from inside the quotes?

    You can do it by regex str.match(/".*?"/g)..

    Or by just doing str.split(`",`).map(url => url.substring(1)). The map part just drops the leading quote.

    Login or Signup to reply.
  4. In this case, you can use regular expressions to parse the string and extract the URLs. Here’s how you can achieve that using JavaScript:

    var str = '"test.com","test2.com","test3.com/123,312","test4.com/123,312"';
    var urls = str.match(/"(.*?)"/g).map(function(url) {
      return url.replace(/"/g, '');
    });
    
    console.log(urls);
    

    Explanation:

    str.match(/"(.*?)"/g) uses a regular expression to match text enclosed in double quotes and returns an array of matched strings.

    .map(function(url) { return url.replace(/"/g, ”); }) removes the double quotes from each matched URL.

    The urls array will contain the extracted URLs.

    This code should work for your example string and handle URLs with commas as well.

    Login or Signup to reply.
  5. You could use a regexp with a lookbehind assertion:

    var str = '"test.com","test2.com","test3.com/123,312","test4.com/123,312"'
    
    console.log(str.match(/(?<=")[^,][^"]+/g));

    Also you could just split the string:

    const str = '"test.com","test2.com","test3.com/123,312","test4.com/123,312"';
    console.log(str.slice(1, -1).split('","'));

    And a benchmark:

    enter image description here

    <script benchmark data-count="1000000">
    
    const str = '"test.com","test2.com","test3.com/123,312","test4.com/123,312"'
    
    // @benchmark vishal_g
    str.match(/"(.*?)"/g).map(function(url) {
      return url.replace(/"/g, '');
    });
    
    // @benchmark Chris Barr
    str.split(`","`).map(s => s.replace(/"/g, ''));
    
    // @benchmark Alexander regex
    str.match(/(?<=")[^,][^"]+/g);
    
    // @benchmark Alexander slice
    str.slice(1, -1).split('","');
    
    </script>
    <script src="https://cdn.jsdelivr.net/gh/silentmantra/benchmark/loader.js"></script>
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search