Splitting long string by exact number. Regex

Hello everyone. Could someone help me to figure out how correctly to use regex to split string by 10 letters. So I’ve got long string like this "00101010100000000010001011101000101110100000111111**********00001011110000111111000010111000101110100000111010". I need to split this string by 10 letters in order to transfer later letters to morse character. I came with this approach

let str="00101010100000000010001011101000101110100000111111****** 00001011110000111111000010111000101110100000111010"; console.log(str.match(/\d{10}/g));

It splits string by 10, however it omits this symbol *. Could you please help me to figure out, how can I split string and symbol by 10? Thanks in advance for your time!
I also tried this

let str = "00101010100000000010001011101000101110100000111111**** 00001011110000111111000010111000101110100000111010"; console.log(str.match(/^\d{10}\W{10}\d{10}$/g));

But it also doesn’t work((
Also for input there’s always numbers 10 and 11 and 10 asterisks. So I need to split long string consisting of numbers and 10 asterisks by 10, then convert it to Morse character.

Isn’t there a function called split that you can use for this?

Edit: slice is the name of the function to do this

1 Like

Yes I know about split function. But I need long string to be divided equally by 10 letters also including symbol *. I tried to use split however didn’t get a result, which I need. Thanks. Could you please share how you may split and will get a result, which I wrote before?

Try slice

1 Like

Slice will cut only part of it. However I need to split long string into an array where each string’s length will be 10. Also including symbol *. If I just do regex with g flag it splits long string into an array, however it skips symbol *. I need this symbol also. I hope explained correctly what I want.

Is the asterisk always the same number or can be random number of asterisk?

1 Like

It’s always asterisk

Exactly 10 asterisks! Always it stands for space later when I will transfer to Morse.

I think you want this /[\d|\*]{10}/gm

1 Like

I didn’t expect to get help so fast! Thank you very much for your time :pray:

1 Like

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.