0

[Tool: Sublime Text 3]

Hello I'm looking for some help with regex, I have very little experience with it so I'm hoping for some help.

I plan to use the regex to help me replace my json file with lots of data in it (over 6k LOC).

I'm looking for a regex that matches the string inside an array in the following json, and split those results (seen in End result):

["Cleric, Ritual Caster, Wizard"]

End result:

["Cleric", "Ritual Caster", "Wizard"]

List of classes (don't know if it would help in the regex): Bard, Cleric, Druid, Paladin, Ranger, Sorcerer, Warlock, Wizard

*Edit: I forgot to add what tool I'm using, using Sublime Text 3 atm, but I can use JavaScript to rewrite the new data and copypasta over my current json file.

Removed invalid json, I'm only looking to edit/fix the array.

6
  • 2
    which tool/lang you're running? Commented Jun 15, 2015 at 16:36
  • "class": ["Ritual Caster"] is it absolute or "class": [any single entry]? Commented Jun 15, 2015 at 16:40
  • What programming language do you use? Is it Java? Commented Jun 15, 2015 at 17:10
  • Don't process JSON with regexps. Commented Jun 15, 2015 at 17:35
  • I'm using Sublime Text 3, trying to replace data in a json file Commented Jun 15, 2015 at 19:19

2 Answers 2

1

In JS you could do:

var classInput = ["Cleric, Ritual Caster, Wizard"];
var classString = classInput[0];  // get the String from the Array
var classOutput = classString.split(/,\s/g); // split into and Array with separate strings

Or shorter:

var classInput = ["Cleric, Ritual Caster, Wizard"];
var classOutput = classInput[0].split(/,\s/g);

example with your list of classes: http://jsfiddle.net/bbmh1a5v/4/

P.S. "class" is often a special word in computer languages, like "id","var" and many more - if possible, try to avoid using "class" as key-word. Better try "characterClass", "profession", ...

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

2 Comments

Umm, why would you replace ,\s first and then split on commas, instead of just splitting on /,\s/ to start with? With regard to your comment about class, I guess you mean using it as a property name, not a "key-word", but actually, there's no problem in modern browsers with using class as a property name.
thanks for your suggestion: you're right - the regex can be used directkly in split - updated my answer
1

Try this:

// Check the first string in the class property (which is assumedly an array) for a comma.
if (characterStats['class'][0].indexOf(',')) {
    // Reassign the property to the result of splitting this string at every comma. If the array has anything beyond the 0th entry, this will remove it.
    characterStats['class'] = yourBlob['class'][0].split(',')
}

Edit:

So we're checking the first entry in the array, which in your example was a big string with multiple commas. We're checking it for the indexOf a comma. If a comma exists in the string (and is past the 0th index), then we will reassign the .class property of your object to the string which is newly split into an array at each comma.

This solution addresses the data model:

{
    class: ['one,big,string,of,words']
}

and turns it into:

{
    class: ['one','big','string','of','words']
}

This solution assumes that you will never have something like this:

{
    class: ['wizard', 'sorcerer, cleric, knave']
}

In which case, you'll have to do some fancier looping and checking.

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.