在字母数字字符串中带有可选点的正则表达式

Regex with optional dots in a alphanumeric string

我正在尝试编写一个正则表达式来匹配可能包含点的固定字母数字字符串。
此正则表达式必须匹配除点之外的所有字符。 我需要在 Javascript search() 中使用它来比较两个字符串。 要查找的词:30A10Z20

所有这些都是正确的匹配:

30A1.0Z2.0
30A.10Z20
3.0.A10.Z20
3.0.A.1.0.Z.2.0.

我已经写了这些但没有成功:

^30A10Z20\.{0,1}?$
^30A10Z20\.?$
^30A10Z20(?=\.)

任何线索或帮助将不胜感激。

我不确定 RegExp 是否是完成我想你想做的事情的最佳方式(根据你的评论:尝试重新表述你的问题)。这个片段是个主意吗?

const findTerm = (word, searchTerm) => 
  word.replace(/[^a-z0-9]/gi, "") === searchTerm;

const aFewStrings = [
  `something 30A1.0Z2.0 etc`,
  `30A.10Z20 hithere`,
  `Hello 3.0.A10.Z20`,
  `The value 3.0.....A.1.0....Z.2.0. may be valid`,
  `As may be the value 3.0@@@A.1.0#&!'Z.2.0.`,
  `Bye 3.0.A.1.0.Z.2.0. ended`,
];

aFewStrings.forEach(s => {
  const words = s.split(" ").map( w => findTerm(w, "30A10Z20") ? `<b>${w}</b>` : w );
  console.log(words.join(" "));
});

如果您想要部分字符串(请参阅您的评论),则必须进行一些解析。类似于:

const findTerm = (word, searchTerm) =>
  RegExp(`(${searchTerm})`, "i").test(word.replace(/[^a-z0-9]/gi, ""));

const toBold = (word, searchTerm) => {
  const word2Parse = [...word];
  const wordPreserved = word2Parse.slice(0);
  const len = searchTerm.length;
  let foundIndices = [];
  let i = 0;

  while (word2Parse.length) {
    const noDots = word2Parse.slice(1).filter(v => !/[^a-z0-9]/i.test(v));
    const next = searchTerm.length > 1 &&  noDots[0] === searchTerm[1];
    const found = searchTerm.length > 1 
        ? word2Parse[0] === searchTerm[0] && next
        : word2Parse[0] === searchTerm[0];
    
    searchTerm = found ? searchTerm.slice(1) : searchTerm;
    found && foundIndices.push(i);
    i += 1;
    word2Parse.shift();
  }


  wordPreserved[foundIndices[0]] = `<b>${wordPreserved[foundIndices[0]]}`;
  wordPreserved[foundIndices.slice(-1)] = `${
        wordPreserved[foundIndices.slice(-1)]}</b>`;
  return wordPreserved.join("");
}

const aFewStrings = [
  `something 30A1.0Z2.0 etc`,
  `30A.10Z20 hithere`,
  `Hello 3.0.A10.Z20`,
  `The value 3.0.....A.1.0....Z.2.0. may be valid`,
  `As may be the value 3.0@@@A.1.0#&!'Z.2.0.`,
  `Bye 3.0.A.1.0.Z.2.0. ended`,
  `3.0.A.1.0.Z.2.....0`,
];


const result = document.querySelector("#result");

let term = `30A1`;
result.appendChild(
  Object.assign(
    document.createElement("p"), {
      innerHTML: `[1 String, search '30A1']: ${
          (aFewStrings[3].split(" ")
            .map(w =>
                findTerm(w, term) ? toBold(w, term) : w)
            .join(" "))}`
    })
);

term = `notfound`;
result.appendChild(
  Object.assign(
    document.createElement("p"), {
      innerHTML: `[1 String, search 'notfound']: ${
          (aFewStrings[1].split(" ")
            .map(w =>
                findTerm(w, term) ? toBold(w, term) : w)
            .join(" "))}`
    })
);

term = `0Z20`;
aFewStrings.forEach(s => {
  const words = s.split(" ").map(w =>
    findTerm(w, term) ? toBold(w, term) : w);
  result.appendChild(
    Object.assign(
      document.createElement("div"), {
        innerHTML: words.join(" ")
      })
  );
});
body {
  margin: 2rem;
  font: normal 12px/15px verdana, arial;
}

b {
  color: red;
}
<div id="result"></div>