เอา Regex รแสดงก่อนเป็นเงื่อนไข

0

คำถาม

ฉันอยากจะส่งผลต่อไปนี้จาก regex แสดง. ฉันอยากจะมีบางอย่างขอ reusability จากพื้นฐานรูปแบบชื่อแฟ้ม

รูปแบบชื่อแฟ้ม

   export const datas = [
      {
        id: 1,
        name: "CODE_SLOT",
        codePosition: 0,
        codeType: "_",
        slotPosition: 1,
        slotType: "_",
      },
      {
        id: 2,
        name: "CODE-SLOT",
        codePosition: 0,
        codeType: "-",
        slotPosition: 1,
        slotType: "-",
      },
    ];

รหัสสำหรับผลิตภัณฑ์รหัส

export const getProductCode = (code, codePosition, codeType) => {
  return (
    code.replace(/\.[^/.]+$/, "").split(codeType)[codePosition] ||
    code.replace(/\.[^/.]+$/, "").split(codeType)[0] ||
    ""
  );
};



const images = [{
    name: "toys-blue_wide.jpg"
}]

const selectedFileNameFormat = datas[0]

const output = images.map((image) => ({
  productCode: getProductCode(
    image?.name,
    selectedFileNameFormat?.codePosition,
    selectedFileNameFormat?.codeType
  ),
}));

console.log(output)

คาดหวังผลส่งออกสำหรับผลิตภัณฑ์รหัส

productCode: toys-blue

รหัสสำหรับตำแหน่ง

export const getSlot = (slot, slotPosition, slotPosition) => {
  return (
    slot.replace(/\.[^/.]+$/, "").split(slotPosition)[slotPosition] ||
    slot.replace(/\.[^/.]+$/, "").split(slotPosition)[0] ||
    ""
  );
};
    const images = [{
    name: "toys-blue_wide.jpg"
}]
const selectedFileNameFormat = datas[0]

const output = images.map((image) => ({
  slotCode: getSlot(
    image?.name,
    selectedFileNameFormat?.codePosition,
    selectedFileNameFormat?.codeType
  ),
}));

คาดหวังผลส่งออกสำหรับตำแหน่ง

slotCode: wide
ecmascript-6 javascript regex
2021-11-23 03:41:23
1

คำตอบที่ดีที่สุด

1

ถ้าฉันเข้าใจคำถามของคุณอย่างถูกต้อง,คุณต้องการเพื่อการวิเคราะห์/คลายแฟ้มจากเป็นข้อความ,i.e."toy-blue_wide.jpg"เป็น productCode ทุกอย่าง ก่อน ที่ "_" ตัวละคร i.e. "toy-blue"และเป็น slotCodeทุกอย่าง หลังจาก ที่ "_" ไม่รวมตัวจแฟ้มเลื่อนเวลาออกไปหน่อย คุณสามารถใช้เป็นโสด REGEX และการจับกุมพวกนี้สองส่วนในพวกเขา respective งเป็นกลุ่ม

/^(.*)_(.*)\./

คนแรก parens จับภาพทุกคนผลิตภัณฑ์รหัสและคนที่สองจับภาพทุกตำแหน่งรหัสมอส ใช้ RexExp.ตัวต้นแบบประมวลผล คุณสามารถประมวลผลการค้นหาทางแต่ละชื่อแฟ้มและกระจายเป็นตารางคู่ลำดับของยากรู้อะไรจะบอกให้หมดเลย

const [, productCode, slotCode] = /^(.*)_(.*)\./.exec(name);

สังเกตเห็นยังว่าเมื่อใช้อาเรย์ destructuring งานเราจะโดดแรกขององค์ นี่เป็นเพราะคนแรกกลับมาจากธาตุ .exec คืนที่เต็มไปด้วข้อความของตัวอักษรตรงกันแต่ทว่า indices 1..n คือจับเป็นกลุ่ม

const images = [{
  name: "toys-blue_wide.jpg"
}];

const codeAndSlotRegex = /^(.*)_(.*)\./;

images.forEach(({ name }) => {
  const [, productCode, slotCode] = /^(.*)_(.*)\./.exec(name);
  console.log({ productCode, slotCode });
});

ปรับปรุง

เพื่ออนุญาตให้สำหรับ dynamically การเลือกชื่อแฟ้มตัวกระจายคำฉันจะกำหนดเฉพาะ REGEX matcher สำหรับแต่ละรูปแบบคุณต้องการเพื่อการวิเคราะห์.

const fileNameFormats = [
  {
    id: 1,
    name: "Code-Slot",
    matcher: /^(.*)-(.*)\./
  },
  {
    id: 2,
    name: "Code_Slot",
    matcher: /^(.*)_(.*)\./
  }
];

Iterate นือตารางคู่ลำดับของภาพและใช้โดยเฉพาะที่เลือกไว้ matcher. กำหนดสำหรับการใช้งานต่อเนื่องตารางคู่ลำดับคุณค่าในกรณีนั้นตรงกับความผิดพลาดและรหัสปริยายและตำแหน่งค่า.

const output = images.map((image) => {
  const [, productCode = "", contentSlot = ""] = selectedFileNameFormat.matcher.exec(
    image?.name
  ) ?? [];
  return {
    productCode,
    contentSlot
  };
});

Edit get-regex-expression-before-a-condition

2021-11-23 07:35:59

@โจเซฟแม่อยู่ไหนนั่นอื่นโพสไม่ได้จริงๆอธิบายอะไรและทั้งหมด codesandbox ทำคือเลือกจากสองตัวเลือกและปูมบันทึกในท้องถิ่นของรัฐ. ฉันไม่แน่ใจว่าคุณอยากให้พวกเราต้องเข้าใจจากมัน
Drew Reese

@โจเซฟโอเคแต่ฉันถามสิ่งที่ตัวเลือกหมายถึง. ถ้าผู้ใช้การเลือก"หัส-ตำแหน่ง"คืออะไรควรจะเกิดขึ้นต่อสิ่งที่ทำการเลือกทางเลือกนั้นหมายถึง? แบบเดียวกันกับว่าของผู้ใช้การเลือก"Code_Slot"? ฉันคิดว่าคุณต้องการที่จะเลือกชื่อแฟ้มรูปแบบการวิเคราะห์. คือที่จริงมันเติง่ายอย่างเพิ่งแยกกับสัญลักษณ์ระหว่า"รหัส"และ"ตำแหน่ง"?
Drew Reese

@โจเซฟฉันเห็นเดี๋ยวนี้ จะให้ผมปรับปรุงคำตอบอยู่ที่นี่งั้นเหรอ?
Drew Reese

ในภาษาอื่นๆ

หน้านี้อยู่ในภาษาอื่นๆ

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................

ดังอยู่ในนี้หมวดหมู่

ดังคำถามอยู่ในนี้หมวดหมู่