0

So to start off, a bit of context. I am pulling data from the following url: "https://webster.cs.washington.edu/pokedex/pokedex.php?pokedex=all" using a GET method. The data returned is a series of Pokemon names and image names in the following format.

Name1:name1.png
Name2:name2.png
...

The list is 151 items long. When I call the typeOf() method "String" is returned, so I am fairly certain it is a String I am dealing with here. What I would like to do is split the String on the delimiters of "\n" and ":".

What I would like:

Name1,name1.png,Name2,name2.png...

After some experimentation with Regex, I found that the Regex to do this was "\n|:". Using this I wrote the following line to split the String apart. I tested this Regex on https://regex101.com and it seems to work properly there.

var splitData = data.split("\n|:");

("data" is the String I receive from the url.)

But instead of splitting the String and placing the substrings into an array it doesn't do anything. (At least as far as I can see.) As such my next idea was to try replacing the characters that were giving me trouble with another character and then splitting on that new character.

data = data.replace("\n", " ");
data = data.replace("/:/g", " ");
var splitData = data.split(" ");

The first line that replaces new line characters does work, but the second line to replace the ":" does not seem to do anything. So I end up with an array that is filled with Strings that look like this.

Name1:name1.png

I can split these strings by calling their index and then splitting the substring stored within, which only confuses me more.

data = data.replace("\n", " ");
var splitData = data.split(" ");
alert(splitData[0].split(":")[1]);

The above code returns "name1.png".

Am I missing something regarding the split() method? Is my Regex wrong? Is there a better way to achieve what I am attempting to do?

Blue
  • 22,608
  • 7
  • 62
  • 92
DJKnarnia
  • 119
  • 5

4 Answers4

1

Right now you are splitting on the string literal "\n|:" but to do a regex you want data.split(/[:\n]/)

The MDN page shows two ways to build a Regex:

var regex1 = /\w+/;
var regex2 = new RegExp('\\w+');
Palu Macil
  • 1,708
  • 1
  • 24
  • 34
  • This solved my problem. Sorry, I'm a bit rusty with Regex (only used it once for a class about a year ago.) I should do a bit more research on String literals and Regex. Thanks. – DJKnarnia Jan 28 '18 at 04:02
  • no problem :) glad to help. Feel free to mark it as the answer if you're satisfied. – Palu Macil Jan 28 '18 at 04:03
0

The following test script was able to work for me. I decided to use the regex in the split instead of trying to replace tokens in the string. It seemed to do the trick for me.

let testResponse = `Abra:abra.png
Aerodactyl:aerodactyl.png`;

let dataArray = testResponse.split(/\n|:/g);
let commaSeperated = dataArray.join(',');

console.log(commaSeperated);
Cogwizzle
  • 550
  • 4
  • 14
0

So you can simply use regex by excluding the quotes all together.

You can look at the documentation here for regular expressions. They give the following examples:

var re = /ab+c/;

var re = new RegExp('ab+c');

See below for your expected output:

var data = `Name1:name1.png
Name2:name2.png`;

var splitData = data.split(/[\n:]/);

console.log(splitData);

//Join them by a comma to get all results
console.log(splitData.join(','));

//For some nice key value pairs, you can reduce the array into an object:

var kvps = data.split("\n").reduce((res, line) => {
  var split = line.split(':');
  return {
      ...res,
      [split[0]]: split[1]
  };
}, {});

console.log(kvps);
Community
  • 1
  • 1
Blue
  • 22,608
  • 7
  • 62
  • 92
-1

I tried and this works good.

 str.split(/[:\n]/)

Here is a plunker.

plunker

Praveen Alluri
  • 307
  • 2
  • 14