正则表达式捕获尾随星号但不包装星号

标签 regex

我正在编写一个正则表达式来替换单词末尾的 *s,其中上标数字代表这些星号的数量,以及行首的星号后跟一个空格然后一句话。也就是说,我让在手机上写脚注变得容易了。编写内容 → 将内容发送到 iOS 快捷方式 → 正则表达式魔法 → 内容带有脚注标记。

但是,由于我经常使用 *foo bar* 来表示强调,所以我不想捕获那些星号。

我以为我有这个正则表达式:

/**
 * (?<=\S)                  -- make sure the thing behind the capture is a not-space
 * (?<!\W\*\w([^*]|\w\*)*?) -- make sure the thing behind the capture is not a not-word character
 *                             followed by an asterisk
 *                             followed by anything that isn't an asterisk
 *                             followed by a letter followed by an asterisk
 *                             e.g. Hello *world*.
 * \*+                      -- 1+ asterisks.  The primary capture for trailing asterisks.
 * (?=[^\w*]|$)             -- make sure the thing following the capture is a not-word-not-asterisk,
 *                             and may be the end of the line
 * |                        -- OR
 * ^\*+(?=\s\S)             -- the start of a line followed by 1+ asterisks (the primary capture)
 *                             followed by a space
 *                             followed by a not-space
 */
const regex = /(?<=\S)(?<!\W\*\w([^*]|\w\*)*?)\*+(?=[^\w*]|$)|^\*+(?=\s\S)/gm;

const transform = m => {
  const superTable = [
    '⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'
  ];

  let str = [];

  // for each digit, add the character for the 1s place then divide by ten
  for (let len = m.length; len; len = (len - len % 10) / 10) {
    str.unshift(superTable[len % 10]);
  }

  return str.join('');
}

/** [input, expectedOutput] */
const testCases = [
  [`A b*** c`, `A b³ c`],
  [`A *b* c*`, `A *b* c¹`],
  [`A *b* *c* d*`, `A *b* *c* d¹`],
  [`A *b* c* d**`, `A *b* c¹ d²`],
  [`** a b c`, `² a b c`],
  [`** a b*** c`, `² a b³ c`],
  [`A *bc* d**`, `A *bc* d²`],
  [`A *b c* d**`, `A *b c* d²`],
];

const results = ['Input\t\t=>\tActual\t\t===\tExpected\t: Success'];
results.push('='.repeat(73));

for (const [input, expected] of testCases) {
  const actual = input.replace(regex, transform);
  const extraSpacing = actual.length < 8 ? '\t' : '';
  const success = actual === expected;
  results.push(`${input}\t=>\t${actual}${extraSpacing}\t===\t${expected}${extraSpacing}\t: ${success}`);
}

console.log(results.join('\n'));

前六个是我第一次写脚本时使用的测试用例。我今天发现的最后两个。事实证明它适用于 *a*(用星号包裹的单个字符)但不适用于 *ab**a b*(2 + 用星号包裹的字符)。

我这辈子都弄不明白我做错了什么,尽管我几周前就写了这个正则表达式。我怀疑这与贪婪或懒惰有关,但我不确定是哪里。

最佳答案

你可以使用

/^\*+(?=\s+\S)|(?<!\s)(?<!\*(?=\S)[^*]*)(\*+)(?![\w*])/gm

参见 regex demo . 详细信息:

  • ^ - 行首
  • \*+(?=\s+\S) - 一个或多个星号后跟一个或多个空格,然后是一个非空格字符
  • | - 或者
  • (?<!\s) - 紧靠左边,不能有空格字符(如果你使用字符字符,\w,你可以在这里使用\b)
  • (?<!\*(?=\S)[^*]*) - 紧靠左边,不可能有*后跟一个非空白字符,然后是零个或多个星号以外的字符
  • \*+ - 一个或多个星号
  • (?![\w*]) - 紧靠右边,不能有单词和*字符。

这是您更新后的 JavaScript 演示:

const regex = /^\*+(?=\s+\S)|(?<!\s)(?<!\*(?=\S)[^*]*)(\*+)(?![\w*])/gm;

const transform = m => {
  const superTable = [
    '⁰', '¹', '²', '³', '⁴', '⁵', '⁶', '⁷', '⁸', '⁹'
  ];

  let str = [];

  // for each digit, add the character for the 1s place then divide by ten
  for (let len = m.length; len; len = (len - len % 10) / 10) {
    str.unshift(superTable[len % 10]);
  }

  return str.join('');
}

/** [input, expectedOutput] */
const testCases = [
  [`A b*** c`, `A b³ c`],
  [`A *b* c*`, `A *b* c¹`],
  [`A *b* *c* d*`, `A *b* *c* d¹`],
  [`A *b* c* d**`, `A *b* c¹ d²`],
  [`** a b c`, `² a b c`],
  [`** a b*** c`, `² a b³ c`],
  [`A *bc* d**`, `A *bc* d²`],
  [`A *b c* d*`, `A *b c* d¹`]
];

const results = ['Input\t\t=>\tActual\t\t===\tExpected\t: Success'];
results.push('='.repeat(73));

for (const [input, expected] of testCases) {
  const actual = input.replace(regex, transform);
  const extraSpacing = actual.length < 8 ? '\t' : '';
  const success = actual === expected;
  results.push(`${input}\t=>\t${actual}${extraSpacing}\t===\t${expected}${extraSpacing}\t: ${success}`);
}

console.log(results.join('\n'));

关于正则表达式捕获尾随星号但不包装星号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69544991/

相关文章:

java - 正则表达式用于检查单词中的重复字母并防止输入字符或符号

java - Regex 类在 GWT 的服务器端不起作用

java - 正则表达式-JAVA

regex - 可选的正则表达式捕获组 - 我缺少什么?

java - 为什么没有边界匹配器 "Beginning of line"正则表达式不匹配?

python - 双转义字符

javascript - RegExp 的类型

javascript - 正则表达式用于有限的数字,无限的中间破折号?

MySQL:在字段(正则表达式)中查找所有带有空 anchor 标记的记录并将其删除

regex - 正则表达式匹配任何字符串powershell