正则表达式替换 2 个字符之间最后一次出现的数字 javascript

regex replace last occurance of number in between 2 chars javascript

我正在尝试替换括号之间每个匹配项的最后一个数字。

所以如果我有 cos(3 * 3)

在此正则表达式之前

result = result.replace(/\d+(?:\.\d+)?/g, x => `${x}deg`)

会把cos(3 * 3)变成cos(3deg * 3deg)

我只需要将最后一个数字更改为deg

所以如果我有 cos(3 * 3) + cos(3 * 2) 它将是 cos(3 * 3deg) + cos(3 * 2deg)

我可以使用什么正则表达式魔术来做到这一点。

您可以像这样简单地使用右大括号替换:

var result = "cos(3 * 3) + cos(3 * 245)"

result = result.replace(/(\d+)(\))/g,'deg)')

console.log(result)

Or just replace the last bracket.

var result = "cos(3 * 90) + sin(3 * 2)"

result = result.replace(/\)/g,'deg)')

console.log(result)

With Decimal add a character class using [] for . and digits

var result = "cos(3 * 3.4) + cos(3 * 245.5)"

result = result.replace(/([\d\.]+)(\))/g,'deg)')

console.log(result)

Any equation at the last number

var result = "3*(1571-sin(12))"

result = result.replace(/(\d\s{0,}\*+\d{0,})(.*\))/g,'deg')

console.log(result)

.../\((?<first>.*?)(?<last>[\d.]+)\)/g...

// see ... [https://regex101.com/r/aDXe3B/1]
const regXLastNumberWithinParentheses =
  /\((?<first>.*?)(?<last>[\d.]+)\)/g;

const sampleData =
`cos(3 * 3) + cos(3 * 24.5), cos(2 * 3) + cos(3 * 2.4)
cos(3 * 3.4) + cos(3 * 45), cos(4 * 2) + cos(3 * 245)`;

console.log(
  sampleData.replace(
    regXLastNumberWithinParentheses,
    (match, first, last) => `(${ first }${ last }deg)`
  )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

第一次编辑

Any idea why in this (3*(2+1+1)-12/5) is coming out as (3*(2+1+1deg)-12/5) it needs to match the last number regardless so it would be (3 *(2+1+1deg) - 23/5deg) – Nik Hendricks

@NikHendricks ... "Any idea why ...?" Yes of cause. Though the above regex matches anything and a final number in between parentheses, it does so just for un-nested parentheses (any nested structures can not be recognized). ... Question: Could one assume that the passed string does always feature validly nested parentheses? If yes, it leads to more straightforward regex replacement patterns because a regex then does not need to also validate the parentheses syntax. – Peter Seliger

...正则表达式更新.../(?<![.\d])(?<number>\d*(?:\.\d+)?)\s*\)/g

// see ... [https://regex101.com/r/aDXe3B/2]
const regXLastNumberBeforeClosingParenthesis =
  /(?<![.\d])(?<number>\d*(?:\.\d+)?)\s*\)/g;

const sampleData =
`cos(3 * 3) + cos(3 * 24.5  ), cos(2 * 3  ) + cos(3 * 2.4)
cos(3 * 3.4) + cos(3 * 45  ), cos(4 * 2  ) + cos(3 * 245)

(3 * (2 + 1 + 1) - 23/5)`;

console.log(
  sampleData.replace(
    regXLastNumberBeforeClosingParenthesis,
    (match, number) => `${ number }deg)`
  )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

第二次编辑

for me the desired result for 3*(1571-sin(1 * 12)) would be 3*(1571deg -sin(1 * 12deg)) the last or only occurrence of a number is replaced with xdeg – Nik Hendricks

...正则表达式更新.../(?<number>(?<!\.)\d+(?:\.\d+)?|(?<![\d.])(?:\.\d+)(?!\.))(?<termination>\s*[-+)])/g

// see ... [https://regex101.com/r/aDXe3B/3]
const regXValidNumberBeforePlusOrMinusOrClosingParenthesis =
  /(?<number>(?<!\.)\d+(?:\.\d+)?|(?<![\d.])(?:\.\d+)(?!\.))(?<termination>\s*[-+)])/g;

const sampleData =
`cos(3 * 3) + cos(3 * 24.5  ), cos(2 * 3  ) + cos(3 * .2.4)
cos(3 * 3.4) + cos(3 * 45  ), cos(4 * .24) + cos(3 * 245 )

(3 *(2 + 1 + 1) - 23/5 )

3 * (1571 - sin(12))`;

console.log(
  sampleData.replace(
    regXValidNumberBeforePlusOrMinusOrClosingParenthesis, 'deg'
  )
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

关于最后的iteration/edit ...以防数字验证不重要(因为输入是可信的) , 其中一个原因可以用不那么严格、因此更简单的正则表达式实现相同的结果,它不完全匹配有效数字...... /(?<dotsAndDigits>[.\d]+)(?<termination>\s*[-+)])/g.

deg 的插入位置只是两个已定义断言之间的位置。

(?<=\([^()]*\d)(?=[^()\d]*\))

https://regex101.com/r/sjw3CX/1

 (?<= \( [^()]* \d )           # Lookbehind open parenth up to last number
 (?= [^()\d]* \) )             # Lookahead no digits and must have a closing parenth

var input = " cos(3 * 3) + cos(3 * 2)"

var output = input.replace(/(?<=\([^()]*\d)(?=[^()\d]*\))/g,'deg')

console.log(output)