Home > Net >  how to convert string to array except some words / regex groups?
how to convert string to array except some words / regex groups?

Time:10-09

I want to convert a string into an array.

Please check js code below, I have written some comments too.

var string = "d-M-[Year] g:i:s a e";

// I can convert this string into Array with two methods "string.split" and "Array.from(string"

string.split(""); // output ['d', '-', 'M', '-', '[', 'Y', 'e', 'a', 'r', ']', ' ', 'g', ':', 'i', ':', 's', ' ', 'a', ' ', 'e']

Array.from(string) // output will same result of string.split("")

But here I want to ignore splitting of [year] from a string

Expected Result: ['d', '-', 'M', '-', '[Year]', ' ', 'g', ':', 'i', ':', 's', ' ', 'a', ' ', 'e']

And likewise Split function should ignore splitting for characters which are start with \ also ignore for characters which are written in [] tags

Ex:

var string = "[Date = ]d \n[Month = ]F\n[Year = ]Y \n[time = ]g:is:s \ne"

// output should be
['[Date = ]', 'd', ' ', '\n', '[Month = ]', 'F', '\n', '[Year = ]', 'Y', ' ', '\n', '[time = ]', 'g', ':', 'i', 's', ':', 's', ' ', '\n', 'e']

I have written one function for this using regular expression but I think it is little lengthy method. if anyone have easy method please share your experience in javascript.

My answer

var string = "[Date = ]d \n[Month = ]F\n[Year = ]Y \n[time = ]g:is:s \ne";


console.log(filterFormat(string));

function filterFormat(string) {
    let filtered = escapeData(string);
    let fs = Array.from(filtered.string); // fs ( filtered string )
    filtered.escaped.map((item, index) => fs.splice((item.index   index), 0, item.str));
    return fs;

    function escapeData(string, escaped=[]) {
        let reg = /(\[.*?\])|(\\.)/gm
        if((match = reg.exec(string)) !== null) {
            string = string.replace(match[0], '')
            escaped.push({str: match[0], index: match.index})
            string = escapeData(string, escaped).string
        }
        return {string, escaped}
    }
}

CodePudding user response:

Spit by the sub string which you don't want to split.

var str = "d-M-[Year] g:i:s a e";

console.log(getExceptionalArray(str, '[Year]'));


function getExceptionalArray(str, except) {

    let mainParts = str.split(except);
    
    return mainParts.flatMap((part, idx, arr) => {    
    
       let subArr = part.split('');
       
       if (idx !== arr.length - 1)
        subArr.push(except)
        
       return subArr;
    })

}

CodePudding user response:

I created a different regex to attempt capturing the expressions inside the brackets (including the brackets too). Then I re-apply the split operation to extract the tokens while ignoring the expressions that did match to obtain the results you expected.

let str = "d-M-[Year] g:i:s a e"; // or "[Date = ]d \n[Month = ]F\n[Year = ]Y \n[time = ]g:is:s \ne"
    
let er = /(\[\w  = \]|\[\w \])/g; // this will match with the things inside the brackets, including the brackets.
let splits = str.split(er);

let result = []
for (let i = 0; i < splits.length; i  ) {
  let s = splits[i]
  if (s.match(er) === null)
result.push(...s.split("")) // re-apply the split operation if it doesn't match
  }
console.log(result)

  • Related