Home > front end >  How would I go about splitting a string into characters - Javascript
How would I go about splitting a string into characters - Javascript

Time:12-31

I want to split a string, such as this:

"Hello $id1, how are you $id2 etc...",

but "$idInt" should not be split:

const test = [
  "$id1",
  "$id2",
  "$id3"
]

let str = "test $id1!";
str = str.split("");
// wanted result: ["t", "e", "s", "t", " ", "$id1", "!"]; 
// actual result: ["t", "e", "s", "t", " ", "$", "i", "d", "1" "!"]

This would take anything from the "test" array and not split it with every sepparate character.

I only searched for other resources but I haven't been able to find something that does what I want this to do exactly.

I also got this code:

    let reg = /\%\[a-zA-Z0-9]\:[0-9] \%/gim;
    let yourstring = 'Some thinger 	ff:13%';
    let matches = reg.exec(yourstring);
    let records = [];
    let pieces = [];
    if (matches != null) {
        for (let [i, match] of Array.from(matches).entries()) {
            let start = yourstring.indexOf(match);
            let end = match.length   start;
            records.push({
                start: start,
                end: end,
                match: match,
            });
        }
        records.sort((a, b) => (a.start > b.start ? 1 : a.start < b.start ? -1 : 0));
        if (records[0].start > 0) {
            records = [{
                start: 0,
                end: 0,
                match: ""
            }].concat(records);
        }
        for (let i = 1; i < records.length; i  ) {
            pieces.push(yourstring.slice(records[i - 1].end, records[i].start).replace(......)); // replace goes here
            pieces.push(records[i].match);
        }
        yourstring = pieces.join("")
    } else {
        yourstring = yourstring.replace(.......) // replace goes here
    } 

but it's regex and I want a set of strings to not be replaced rather than this here with regex

CodePudding user response:

I would propose using match instead of split:

let s = "Hello %id:1%, how are you %id:2% etc...";

let result = s.match(/%id:\d %|./gs);

console.log(result);

CodePudding user response:

You should use the split function like this:

let str1 = "test %id:1%!"
let arr1 = str1.split('%')
let test = [];
// arr1 will become ['test ','id:1','!']
// every alternate one will become one of the usable one.
arr1.forEach((x,index)=>{
    if (index%2 == 1) {
        // push in test and add the '%'s back in.
        test.push(`%${x}%`)
    }
})
        

CodePudding user response:

Trincot's answer is better but I already wrote this so here it is...

var str = "Hello %id:1%, how are you %id:2% etc...";

console.log(splitStr(str));

function splitStr(str) {
  var is_var = false;
  var chars = [];
  var group = '';
  for (var i = 0; i < str.length; i  ) {
    let char = str[i];
    if (is_var) {
      group  = char;
      if ('%' == char) {
        chars.push(group);
        group = '';
        is_var = false;
      }
    } else {
      if ('%' == char) {
        group  = char;
        is_var = true;
      }else{
        chars.push(char);
      }
    }
  }
  return chars;
}

  • Related