0

Trying to extract the alphanumeric parts of a variable length string with a known pattern using Regex in Google Application Scripts. Pattern repeats n times as follows (XXXs are groups of Alphanumeric characters): XX-XXX-X-XX-XX-........ for example ABC-AB or ABCD-AB-ABC-AA I want to extract the alphanumeric parts into an Array if possible like e[0] = ABCD e[1] = AB e[2] = ABC .....

I tried repeated \w+ but that requires knowing the possible lengths of string. See below. Is there a way for Regex to process varying size strings? See my example code below:

var data1 = 'ABC-AB';
var data2 = 'ABCD-AB-ABCD-AA';

var regex1 = new RegExp(/(\w+)-(\w+)/); 
var regex2 = new RegExp(/(\w+)/);

e = regex1.exec(data1); //stores ABC and AB as separate array elements.

This is fine but won't work on a string with larger size

e = regex2.exec(data2); //stores ABCD only as a single array element "ABCD"
1
  • Match the repeated string and split on - Commented Oct 19, 2020 at 20:08

1 Answer 1

1

To match any length of kebab case letters:

var regex1 = new RegExp(/\w+(-\w+)*/)

For each of the matches found, split the result on dashes to get your array.

var array = found.split("-")
Sign up to request clarification or add additional context in comments.

4 Comments

I tried your Regex1 suggestion: var regex1 = new RegExp(/\w+(-\w+)*/) on 'ABC-AB-ABCD-AA' and it resulted in "-AA" getting grouped in my array. I am not clear on how you intended the code to be used. Please clarify.
As per answer, regex1 will find all kebab case sequences, for example it will match ABC-AB-ABCD-AA from foo ABC-AB-ABCD-AA bar. Having extracted that match (I’m not familiar with Google apps script, so I don’t know the syntax, but I’m sure it can be done), you then split the extracted match on hyphen, which will result in the array [ABC, AB, ABCD, AA]
Thanks, but my question is not about extracting the kebab sequence from a random sequence. I already have a kebab sequence to being with. I am trying to find a Regex expression that will extract the Alphanumeric parts of the Kebab sequence into an array. So from ABC-AB-ABCD-AA-(varying length) into [ABC, AB, ABCD, AA, (varying length)]
But you introduced me to .split method, which is an easier solution. So all I have to do is e = data2.split('-'); and I get the array I want. It is a curious question though if Regex can be applied to a string of variable lengths.

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.