Javascript replace() with $1
Javascript replace() with $1
输入字符串为"thisISpinalTap"。我用它的小写版本替换了每个大写字母。我还想在信前面加一个-,以获得脊椎病例的效果"这是脊椎穿刺"。
我写了以下代码行:
str = str.replace(/([A-Z])/g, '-').toLowerCase();
这将每个大写字母替换为-以产生"This-s-pinal-ap"。这不是预期的效果。
我读到使用1美元可以产生我想要的效果,它确实做到了。
str = str.replace(/([A-Z])/g, '-$1').toLowerCase();
$1是如何将-附加到字符串中,而不是替换大写字母的?
$1
是正则表达式中捕获的第一组。
因此,在这种情况下,-$1
将替换与-
匹配的内容,然后是([A-Z])
中捕获的大写字母。
相关文章:
- Replace a "href" element with javascript
- How to replace space with " "
- Javascript replace() with $1
- Trouble with replace() and § JavaScript
- Javascript str.replace with variables
- 在 jQuery 中的 replace With 上添加 SlideUp 和 Slidedown 动画
- 使用Replace With返回数组的最后一个值
- How to replace new line break with ' '
- Replace HTML with JavaScript
- Replace spaces with <br /> on a date
- Replace @mention with link
- Replace , with <br>
- 我正在做codewars挑战Replace With Alphabet Position,但我不理解它
- to replace " " with
- jQuery replace click() with document.ready()
- Having trouble with javascript Replace()
- Javascript | Can't replace with String.replace()
- Replace anchor with ajax
- Replace with <br>
- 使用Replace with in查询来替换多个元素