简体   繁体   English

JavaScript匹配(后跟一个数字

[英]JavaScript match ( followed by a digit

In string n+n(n+n) , where n stands for any number or digit, I'd like to match ( and replace it by *( , but only if it is followed by a number or digit. 在字符串n+n(n+n) ,其中n代表任何数字或数字,我想进行匹配(并将其替换为*( ,但前提是它后面跟随数字或数字。

Examples: 例子:

  • I'd like to change 2+22(2+2) into 2+22*(2+2) , 我想将2+22(2+2)更改为2+22*(2+2)
  • I'd like to change -1(3) into -1*(3) , 我想将-1(3)更改为-1*(3)
  • 4+(5/6) should stay as it is. 4+(5/6)应该保持原样。

This is what I have: 这就是我所拥有的:

var str = '2+2(2+2)'.replace(/^[0-9]\(/g, '*(');

But it doesn't work. 但这是行不通的。 Thanks in advance. 提前致谢。

Remove the ^ , and group the digits: 删除^ ,然后将数字分组:

'2+2(2+2)'.replace(/([0-9])\(/g, '$1*(')
'2+2(2+2)'.replace(/(\d)\(/g, '$1*(')    //Another option: [0-9] = \d

Suggestion : 2. is often a valid number (= 2 ). 建议2.通常是有效数字(= 2 )。 The following RegExp removes a dot between a number and a parenthesis. 下面的RegExp删除数字和括号之间的点。

'2+2(2+2)'.replace(/(\d\).?\(/g, '$1*(') //2.(2+2) = 2*(2+2)

Parentheses create a group, which can be referenced using $n , where n is the index of the group: $1 . 括号创建一个组,可使用$n进行引用,其中n是组的索引: $1

You started your RegExp with a ^... , which means: Match a part of the string which starts with ... . 您以^...开头了RegExp,这意味着:匹配以...开头的字符串的一部分。 This behaviour was certainly not intended. 这种行为当然不是故意的。

var str = '2+2(2+2)+3(1+2)+2(-1/2)'.replace(/([0-9])\(/g, '$1*(');

http://jsfiddle.net/ZXU4Y/3/ http://jsfiddle.net/ZXU4Y/3/

This follows what you wrote (the bracket must follow a number). 这跟您所写的一样(括号内必须跟数字)。

So 4( will be changed to 4*( it could be important for example for 4(-1/2) 因此4(将更改为4*(例如对于4(-1/2)可能很重要

You can use capturing groups and backreferences to do it. 您可以使用捕获组和反向引用来执行此操作。

Check out this page , under "Replacement Text Syntax" for more details. 请查看此页面的 “替换文本语法”下的更多详细信息。

Here's a fiddle that does what you ask for. 这是一个小提琴可以满足您的要求。

Hope this helps. 希望这可以帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JavaScript 正则表达式:仅当数字后跟“-”时才匹配“-”,但只有单个“-”时不匹配 - JavaScript Regex: Match "-" only if a digit is followed by "-", but not match when there is only single "-" 如何在开头匹配 0 或 + 后跟 1 到 3 位数字,但不能同时匹配两者? - How to match either 0 or + followed by 1 to 3 digit number at the beginning but not both? regex--如何匹配括号后跟非数字字符? - regex--how to match a parentheses followed by non-digit characters? 正则表达式匹配字符的所有实例,除非后面跟数字或它本身 - Regex match all instances of character except when followed by a digit or is by itself 使用javascript显示特定字符​​,然后显示字符串中的数字 - Display a specific character followed by a digit from a string using javascript javascript正则表达式匹配单词,后跟数字 - javascript regular expression to match a word and followed by digits Javascript正则表达式匹配k,后跟数字0 - Javascript Regex match k that is not followed by a number 0 Javascript正则表达式匹配字符串后跟数字? - Javascript regular expression match on string followed by number? JavaScript正则表达式匹配数字后跟字母 - JavaScript Regular Expression to match a number followed by a letter 如何在javascript中有效地匹配字符串中间的数字? - How to match digit in middle of a string efficiently in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM