Home > Software engineering >  regex replace for multiple string array javascript
regex replace for multiple string array javascript

Time:01-14

I have a array of string and the patterns like #number-number

If the # and single digit number before by hyphen then replace # and add 0

If the # and two or more digit number before by hyphen then replace remove #

If there is no # and single digit number before by hyphen then add 0

I got stuck and how to do in javascript

In #number,

if # and number is two or more digits before hyphen(-) remove # only 
eg
#162-7878 should be 162-7878, #12-4598866 should be 12-4598866)


if # and number is single digit before hyphen(-) remove # and add 0 
eg
#1-7878 should be 01-7878

If there is no # and single digit number before hyphen add 0
eg
1-7878 should be 01-7878
let arrstr=["#12-1676","#02-8989898","#676-98908098","12-232","02-898988","676-98098","2-898988"]

for(let st of arrstr)
 console.log(st.replace(/#?(\d)?(\d-)/g ,replacer))
 
 function replacer(match, p1, p2, offset, string){
  let replaceSubString = p1 || "0";
  replaceSubString  = p2;
  return replaceSubString;
 }

CodePudding user response:

Something like this could work.

let arrstr = ["#12-1676","#02-8989898","#676-98908098","12-232","02-898988","676-98098","2-898988"]

for (const st of arrstr) {
 console.log(st.replace(/(^#?)(\d )(?=-)/ ,replacer))
}

function replacer(match, p1, p2) {
  if (p1 === '#') {
    if (p2.length === 1) {
      return '0'   p2;
    } else {
      return p2;
    }
  } else {
    return p2;
  }
}

CodePudding user response:

let list_of_numbers =["#1-7878", "#162-7878", "#12-1676","#02-8989898","#676-98908098","12-232","02-898988","676-98098","2-898988"]

const solution = () => {
    let result = ''
    for (let number of list_of_numbers) {
        let nums = number.split('-')
        if (nums[0][0] == '#' && nums[0].length > 2) {
            result = `${nums[0].slice(1, number.length-1)}-${nums[1]}`
            console.log(result)
        } else if (nums[0][0] == '#' && nums[0].length == 2) {
            result = `${nums[0][0] = '0'}${nums[0][1]}-${nums[1]}`
            console.log(result)
        } else {
            console.log(number)
        }
    }
}

CodePudding user response:

Using the unary operator, here's a two liner replacer function.

const testValues = ["#162-7878", "#12-4598866", "#1-7878"];
const re = /#(\d ?)-(\d )/;

for(const str of testValues) {
  console.log(str.replace(re, replacer));
}

function replacer(match, p1, p2) {
  p1 =  p1 < 10 ? `0${p1}` : p1;
  return `${p1}-${p2}`; 
}

CodePudding user response:

I think a simple check is what you should do with the match function.

let arrstr=["#12-1676","#0-8989898","#676-98908098","12-232","02-898988","676-98098","2-898988"];
const regex = /#\d-/g;
for(i in arrstr){
    var found = arrstr[i].match(regex);
    if(found){
      arrstr[i]=arrstr[i].replace("#","0")
    }else{
      arrstr[i]=arrstr[i].replace("#","")
    }
}
console.log(arrstr);

or if you really want to stick with the way you have it.

let arrstr=["#12-1676","#02-8989898","#6-98908098","12-232","02-898988","676-98098","2-898988"]

for(let st of arrstr)
 console.log(st.replace(/#(\d)?(\d-)/g ,replacer))
 
 function replacer(match, p1, p2, offset, string){
  let replaceSubString = p1 || "0";
  replaceSubString  = p2;
  return replaceSubString;
 }

remove the '?' from the regex so its not #? but just #

  •  Tags:  
  • Related