简体   繁体   English

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

[英]regex replace last occurance of number in between 2 chars javascript

I am trying to replace the last number of each match in between parenthesis.我正在尝试替换括号之间每个匹配项的最后一个数字。

so if I had cos(3 * 3)所以如果我有cos(3 * 3)

before this regex在这个正则表达式之前

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

would turn cos(3 * 3) into cos(3deg * 3deg)会将cos(3 * 3)变成cos(3deg * 3deg)

I need only the last number to be changed to deg我只需要将最后一个数字更改为deg

So if I had cos(3 * 3) + cos(3 * 2) it would be cos(3 * 3deg) + cos(3 * 2deg)所以如果我有cos(3 * 3) + cos(3 * 2)它将是cos(3 * 3deg) + cos(3 * 2deg)

What regular expression magic could I use to do this.我可以使用什么正则表达式魔术来做到这一点。

You can simply use the closing braces to replace like this:您可以简单地使用右大括号来替换这样的:

 var result = "cos(3 * 3) + cos(3 * 245)" result = result.replace(/(\d+)(\))/g,'$1deg)') 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 .使用 Decimal 添加字符 class,使用[] for . and digits和数字

 var result = "cos(3 * 3.4) + cos(3 * 245.5)" result = result.replace(/([\d\.]+)(\))/g,'$1deg)') console.log(result)

Any equation at the last number最后一个数字的任何方程式

 var result = "3*(1571-sin(12))" result = result.replace(/(\d\s{0,}\*+\d{0,})(.*\))/g,'$1$2deg') console.log(result)

... /\((?<first>.*?)(?<last>[\d.]+)\)/g ... ... /\((?<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; }

1st Edit第一次编辑

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知道为什么在这个 (3*(2+1+1)-12/5) 中会出现 (3*(2+1+1deg)-12/5) 它需要匹配最后一个数字,无论如何它都会是 (3 *(2+1+1deg) - 23/5deg) – Nik Hendricks

@NikHendricks... "Any idea why...?" @NikHendricks…… “知道为什么……吗?” 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 – 彼得塞利格

... regex update... /(?<.[?\d])(?<number>\d*(:.\?\d+)?)\s*\)/g ... 正则表达式更新... /(?<.[?\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; }

2nd Edit第二次编辑

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对我来说,3*(1571-sin(1 * 12)) 的预期结果将是 3*(1571deg -sin(1 * 12deg)) 最后一次或唯一一次出现的数字被替换为 xdeg – Nik Hendricks

... regex update... /(?<number>(?<.\?)\d+(:.\?\d+)?|(.<?[\d:])(.?\.\d+)(?!\.))(?<termination>\s*[-+)])/g ... 正则表达式更新... /(?<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, '$1deg$2' ) );
 .as-console-wrapper { min-height: 100%;important: top; 0; }

Regarding the last iteration/edit ... in case number validation is not important (because the input is trusted), one of cause could achieve the same result with a less strict, thus simpler, regex which does not exclusively match valid numbers... /(?<dotsAndDigits>[.\d]+)(?<termination>\s*[-+)])/g .关于最后一次迭代/编辑......如果数字验证不重要(因为输入是可信的),原因之一可以用不那么严格,因此更简单的正则表达式实现相同的结果,它不完全匹配有效数字.. . /(?<dotsAndDigits>[.\d]+)(?<termination>\s*[-+)])/g .

The insertion position for deg is just a place between two defined Assertions. deg的插入 position 只是两个定义的断言之间的一个地方。

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

https://regex101.com/r/sjw3CX/1 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)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM