简体   繁体   English

正则表达式匹配不以其他字符串开头的任何字符串

[英]Regex match any string that isn't preceded by another string

I suck at regex and I've been trying to get through this problem. 我很喜欢正则表达式,我一直在努力解决这个问题。 The problem is that I have a string and I want to match any "+" or "-" that aren't preceded by a "^". 问题是我有一个字符串,并且要匹配没有以“ ^”开头的任何 “ +”或“-”。

Example string: 示例字符串:

100x^-3+2x-10

The string above would be mathematically formatted or read as "one hundred times x to the -3 power, plus two times x, minus ten". 上面的字符串将被数学格式化或读为“ x --3幂的100倍,再加上x的两倍,减去十”。

And I want to match the "+" before "2x" and the "-" before the "10" but not the "-" after "100x^". 我想匹配“ 2x”之前的“ +”和“ 10”之前的“-”,而不是 “ 100x ^”之后的“-”。 I hope it's not that confusing. 我希望那不是那么令人困惑。 I have tried the following reg, with no luck: 我尝试了以下reg,但没有运气:

[^\^][\+|\-]

Obviously I'm missing a big detail somewhere. 显然我在某处缺少一个大细节。

您可以通过反转字符串,然后与/[\\+\\-](?!=\\^)/进行匹配,来使用模拟回头的古老技巧。

它可以在chrome控制台中正常运行,无论如何您都可以尝试: "100x^-3+2x-10".match(/[^\\^$]([\\-|\\+])/ig) ,我建议您在Google Chrome浏览器的控制台中测试您的正则表达式。

If you need to split by + or - : 如果需要用+-分开:

"100x^-3+2x-10".replace(/([^^])[-+]/g,'$1@').split('@')
Output: ["100x^-3", "2x", "10"]

I used @ as a temp char (but you can use a unique set of chars) to mark the signs not prefixed by ^ then safely split by the chosen temp char. 我将@用作临时字符(但是您可以使用一组独特的字符)来标记没有以^为前缀的符号,然后用所选的临时字符安全地分开。

试一下:

/(?<!\^)[+-]/

暂无
暂无

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

相关问题 正则表达式以匹配以特定字符串开头并以特定字符串结尾的字符串的内容 - Regex to match contents of a string preceded by particular string and ended with a particular string 如何匹配字符串包含特定文本,前面是正则表达式中的字母数字和其他字符的任意组合 - How to match a string contain specific text preceded by any combination of alphanumerics and other charcters in regex 如果该子字符串前面没有特定字符串并忽略整个字符串,则正则表达式匹配该子字符串? - Regex match a substring if that substring is not preceded by a specific string and ignore the whole string? 正则表达式 - 匹配字符串前面没有另一个字符串 (JavaScript) - Regular Expression - Match String Not Preceded by Another String (JavaScript) 正则表达式 - 匹配字符串中的任何数字 - Regex - match any digit in a string 正则表达式:在另一个字符串中间匹配url字符串 - Regex: Match url string in the middle of another string 正则表达式仅匹配另一个字符串内的字符串 - Regex to match a string only inside another string JS中的正则表达式:查找一串以字符开头的数字 - Regex in JS: find a string of numbers that are preceded by a character 如果前面没有反斜杠,如何匹配粗体降价? - How to match bold markdown if it isn't preceded with a backslash? 正则表达式匹配任何字符串的前半部分? - regex to match first half of any string?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM