如何在Visual Studio 2017中用正则表达式给每行文本加<li>标签?
Got it, let's sort out this regex find-and-replace issue for you. The problem with your original setup is that you didn't capture the line content into a group—so $1 was empty, leading to empty <li> tags. Here's the step-by-step fix:
- Open the Find and Replace dialog (press
Ctrl+H). - Make sure the Use Regular Expressions toggle is enabled (it's the button with the
.*icon in the dialog toolbar). - Set your Find what field to:
^(.*)$- The parentheses
()wrap the entire line content into the first capture group, which we'll reference later. ^matches the start of a line,.*matches any character (except newlines) zero or more times,$matches the end of a line.
- The parentheses
- Set your Replace with field to:
<li>$1</li>$1pulls in the content we captured with the parentheses in the find pattern.
- Click Replace All to apply this to every line in your file.
Bonus: Skip Empty Lines
If you don't want to add <li> tags to blank lines, adjust your find pattern to:^(.+)$
The + instead of * ensures we only match lines with at least one character.
Just a quick note: Visual Studio 2017 uses its own flavor of regex, so capture groups work with standard parentheses () and you reference them with $1, $2, etc.—which you already had right, you just missed wrapping the line content in a group!
内容的提问来源于stack exchange,提问作者Rod
相关产品推荐
相关产品推荐

