Home > OS >  How to parse an arguments containing a url from a string so that url doesn't split on query par
How to parse an arguments containing a url from a string so that url doesn't split on query par

Time:12-15

I'm building a desktop app and it accepts a number of flags through cli args.
Args Looks like this "configURI=launch://test.domain.com:7089/jav_config.ws,gameWorld=2,character=Test"
Previously I parsed it with regexp:
https://regex101.com/r/V8a2Kr/1

export const COMMA_DELIMITEDREGEXP = /\w =(?:[[a-zA-Z0-9 []:/.~,=-]]|[a-zA-Z0-9 _:/.~-])|\w /g; 
const args = "configURI=launch://test.domain.com:7089/jav_config.ws,gameWorld=2,character=Test"

args.match(COMMA_DELIMITEDREGEXP)

// Returns object with 3 entries
// 0: "configURI=launch://test.domain.com:7089/jav_config.ws"
// 1: "gameWorld=2"
// 2: "characther=Test"

But I found a bug with parsing URL string if it has any query params on it.

e.x. configURI=test.domain.com:7089/jav_config.ws?devbuild=1

const args = "configURI=launch://test.domain.com:7089/jav_config.ws?devbuild=1,gameWorld=2,character=Test"

args.match(COMMA_DELIMITEDREGEXP)

// Returns object with 4 entries
// 0: "configURI=launch://test.domain.com:7089/jav_config.ws"
// 1: "devbuild=1"
// 2: "gameWorld=2"
// 3: "characther=Test"

I want to not break URL string, hence parsing result should be

// 0: "configURI=launch://test.domain.com:7089/jav_config.ws?devbuild=1"
// 1: "gameWorld=2"
// 2: "characther=Test"

I could add another regexp only for the URL matching and use existing regexp to parse other entries.
But I wonder if there is any way to somehow modify existing regexp so it will not split URLs with query params into two entries?

CodePudding user response:

You can split the input string by any comma that is succeeded by a parameter name:

,(?=\w =)

const input = "configURI=launch://test.domain.com:7089/jav_config.ws?devbuild=1&characters=[Test, Test2],gameWorld=2,character=Test";

const regex = /,(?=\w =)/g;

const parts = input.split(regex);

console.log(parts);

Try it.

CodePudding user response:

Assuming there is no nested level and taking coma separated lists, if you can enforce these are enclosed in brackets, you may use something like this:

[^=,] =(?:\[[^\]] \]|[^,] )(?![^,])

Demo

  • Related