Home > Blockchain >  REGEX: remove left of string until special character
REGEX: remove left of string until special character

Time:07-26

Given the following value ora:hashedPassword I want a regex to remove the whole left string until the : so is left with hashedPassword

I am doing the following currently but only accepts one value to replace, in my current scenario, the array value could contain multiple string patterns (ora:value1, org:value2,pfog1r0:value2)

var schemaArr = ['ora:hashedPassword', 'org:DISTRIB_VALUES_OTH', 'org:categoryTrackingLogStats', 'org:champsPersoAutres', 'org:champsPersoBoutique', 'org:deliveryDOLogStats', 'org:ebNotif', 'org:familleTypologie', 'org:filActuNewBox', 'org:lovDevice', 'org:newBoxNotif', 'org:potentielFibre', 'org:tempsSaison', 'pfog1r0:BRIEF_DELIVERY', 'pfog1r0:BRIEF_DELIVERY_DO', 'pfog1r0:BRIEF_DELIVERY_OFFER_RANGE', 'pfog1r0:BRIEF_DELIVERY_TARGET', 'pfog1r0:BRIEF_DELIVERY_TARGET_DO', 'pfog1r0:BRIEF_DELIVERY_TGT_OFRG', 'pfog1r0:OPERATION_PRT_WAVES', 'pfog1r0:OPERATION_TMK_SUPPLIER'];

var arrayLength = schemaArr.length;

for (var i = 0; i < arrayLength; i  ) {
  console.log(schemaArr[i], "auto_"   schemaArr[i].replace("org:", '')   "_seq")
}

CodePudding user response:

A regex to replace all characters leading up to the colon could be as simple as /.*?:/

var schemaArr = ['ora:hashedPassword', 'org:DISTRIB_VALUES_OTH', 'org:categoryTrackingLogStats', 'org:champsPersoAutres', 'org:champsPersoBoutique', 'org:deliveryDOLogStats', 'org:ebNotif', 'org:familleTypologie', 'org:filActuNewBox', 'org:lovDevice', 'org:newBoxNotif', 'org:potentielFibre', 'org:tempsSaison', 'pfog1r0:BRIEF_DELIVERY', 'pfog1r0:BRIEF_DELIVERY_DO', 'pfog1r0:BRIEF_DELIVERY_OFFER_RANGE', 'pfog1r0:BRIEF_DELIVERY_TARGET', 'pfog1r0:BRIEF_DELIVERY_TARGET_DO', 'pfog1r0:BRIEF_DELIVERY_TGT_OFRG', 'pfog1r0:OPERATION_PRT_WAVES', 'pfog1r0:OPERATION_TMK_SUPPLIER'];

var arrayLength = schemaArr.length;

for (var i = 0; i < arrayLength; i  ) {
  console.log(schemaArr[i], "auto_"   schemaArr[i].replace(/.*?:/, '')   "_seq")
}

CodePudding user response:

You can use match and (?<=:) lookbehind approach, to find the text after :

var schemaArr = ['ora:hashedPassword', 'org:DISTRIB_VALUES_OTH', 'org:categoryTrackingLogStats', 'org:champsPersoAutres', 'org:champsPersoBoutique', 'org:deliveryDOLogStats', 'org:ebNotif', 'org:familleTypologie', 'org:filActuNewBox', 'org:lovDevice', 'org:newBoxNotif', 'org:potentielFibre', 'org:tempsSaison', 'pfog1r0:BRIEF_DELIVERY', 'pfog1r0:BRIEF_DELIVERY_DO', 'pfog1r0:BRIEF_DELIVERY_OFFER_RANGE', 'pfog1r0:BRIEF_DELIVERY_TARGET', 'pfog1r0:BRIEF_DELIVERY_TARGET_DO', 'pfog1r0:BRIEF_DELIVERY_TGT_OFRG', 'pfog1r0:OPERATION_PRT_WAVES', 'pfog1r0:OPERATION_TMK_SUPPLIER'];

const result = schemaArr.map(i => i.match(/(?<=:)\w /)[0])

console.log(result)

CodePudding user response:

So the regex is very short: /. :/.

  • . - one or more characters
  • : followed by a colon

There are couple of ways to iterate over these strings depending on whether you want to mutate the existing array, or create a new array of changed elements.

  1. Mutate the existing array

const schemaArr = ['ora:hashedPassword', 'org:DISTRIB_VALUES_OTH', 'org:categoryTrackingLogStats', 'org:champsPersoAutres', 'org:champsPersoBoutique', 'org:deliveryDOLogStats', 'org:ebNotif', 'org:familleTypologie', 'org:filActuNewBox', 'org:lovDevice', 'org:newBoxNotif', 'org:potentielFibre', 'org:tempsSaison', 'pfog1r0:BRIEF_DELIVERY', 'pfog1r0:BRIEF_DELIVERY_DO', 'pfog1r0:BRIEF_DELIVERY_OFFER_RANGE', 'pfog1r0:BRIEF_DELIVERY_TARGET', 'pfog1r0:BRIEF_DELIVERY_TARGET_DO', 'pfog1r0:BRIEF_DELIVERY_TGT_OFRG', 'pfog1r0:OPERATION_PRT_WAVES', 'pfog1r0:OPERATION_TMK_SUPPLIER'];

for (let i = 0; i < schemaArr.length; i  ) {
  schemaArr[i] = schemaArr[i].replace(/. :/, '');
}

console.log(schemaArr);

  1. Create a new updated array using map to iterate over each element and returning a changed element to the output array.

const schemaArr = ['ora:hashedPassword', 'org:DISTRIB_VALUES_OTH', 'org:categoryTrackingLogStats', 'org:champsPersoAutres', 'org:champsPersoBoutique', 'org:deliveryDOLogStats', 'org:ebNotif', 'org:familleTypologie', 'org:filActuNewBox', 'org:lovDevice', 'org:newBoxNotif', 'org:potentielFibre', 'org:tempsSaison', 'pfog1r0:BRIEF_DELIVERY', 'pfog1r0:BRIEF_DELIVERY_DO', 'pfog1r0:BRIEF_DELIVERY_OFFER_RANGE', 'pfog1r0:BRIEF_DELIVERY_TARGET', 'pfog1r0:BRIEF_DELIVERY_TARGET_DO', 'pfog1r0:BRIEF_DELIVERY_TGT_OFRG', 'pfog1r0:OPERATION_PRT_WAVES', 'pfog1r0:OPERATION_TMK_SUPPLIER'];

const updated = schemaArr.map(el => {
  return el.replace(/. :/, '');
});

console.log(updated);

An alternative method (as mentioned in the comments) might be to just split the string on the colon to create an array, and then return the second element

const schemaArr = ['ora:hashedPassword', 'org:DISTRIB_VALUES_OTH', 'org:categoryTrackingLogStats', 'org:champsPersoAutres', 'org:champsPersoBoutique', 'org:deliveryDOLogStats', 'org:ebNotif', 'org:familleTypologie', 'org:filActuNewBox', 'org:lovDevice', 'org:newBoxNotif', 'org:potentielFibre', 'org:tempsSaison', 'pfog1r0:BRIEF_DELIVERY', 'pfog1r0:BRIEF_DELIVERY_DO', 'pfog1r0:BRIEF_DELIVERY_OFFER_RANGE', 'pfog1r0:BRIEF_DELIVERY_TARGET', 'pfog1r0:BRIEF_DELIVERY_TARGET_DO', 'pfog1r0:BRIEF_DELIVERY_TGT_OFRG', 'pfog1r0:OPERATION_PRT_WAVES', 'pfog1r0:OPERATION_TMK_SUPPLIER'];

const updated = schemaArr.map(el => {
  return el.split(':')[1];
});

console.log(updated);

  • Related