JavaScript 正则表达式 : Inject specific number at specific position in a string

标签 javascript regex

var s = '-10px -10px';
var n = '33px';

我需要一个产生这个结果的正则表达式:'-10px 33px'

类似于:s.replace(???, n)

最佳答案

如果您只想替换字符串末尾的 -10,您可以这样做:

s = s.replace(/-10px$/, n);

如果您希望能够替换任何 数字,那么您可以使用\d 代替:

/-?\d+px$/

关于JavaScript 正则表达式 : Inject specific number at specific position in a string,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4904837/

相关文章:

javascript - 在 Node.js 中使用 jsonwebtokens 进行身份验证

javascript - 正则表达式匹配用大括号包裹的单词

regex - 使用 'regex_replace' Ansible过滤器匹配文字

regex - 从 'system' 命令返回的字符串的编码

php - preg_match_all html 标签,双引号或单引号中的标签除外

asp.net - 需要使用 javascript 进行登录验证的帮助

javascript - 导出 Excel 表格

javascript - React Native Button onPress 不起作用

javascript - 分析 Threejs 应用程序

Javascript正则表达式阻止匹配内部标签