Consider strings with this format:
JavaScript
x
2
1
id-string1-string2-string3.extension
2
where id, string1, string2 and string3 can be string of variable length, and extension is an image extension type.
For example, two possible strings could be:
JavaScript
1
4
1
Il2dK-Ud2d9-Kod2d-d9dwo.jpg
2
3
j54fwf3da-7jrg-9eujodww-kio98ujk.png
4
I need tokenizer method in JavaScript for an express/nodejs API that takes these strings in input and outputs an object with this format:
JavaScript
1
2
1
{a: id-string1-string2, b: string3, c: extension}
2
For the example strings this tokenizer should then output:
JavaScript
1
4
1
{a: Il2dK-Ud2d9-Kod2d, b: d9dwo, c: jpg}
2
3
{a: j54fwf3da-7jrg-9eujodww, b: kio98ujk, c: png}
4
I think this can be done with regex. I tried to use the following regex match(/[^-]+/g), but this tokenize every substring, I need a way to skip the first 2 char “-” but couldn’t find it out.
Do you have any ideas? Or could you provide me a better solution instead of using regex? Thanks very much!
Advertisement
Answer
You can achieve this using spit
as:
JavaScript
1
5
1
const str = 'Il2dK-Ud2d9-Kod2d-d9dwo.jpg';
2
const [restStr, c] = str.split('.');
3
const [a, b] = restStr.split(/-([a-z0-9]+$)/);
4
const result = { a, b, c };
5
console.log(result);