简体   繁体   English

Javascript 字符之间单词的正则表达式

[英]Javascript regex for words between characters

As the title states, I tried making regex for %George%<Croissant>|2|10.3$正如标题所述,我尝试为%George%<Croissant>|2|10.3$制作正则表达式

The point is it has to match only the name between the both % and the product between both <> and then the numbers between |关键是它必须只匹配两个%之间的名称和两个<>之间的产品,然后是|之间的数字。

However, if the name doesn't start with capital letters followed by only lowercase letters, it is invalid hence not matched.但是,如果名称不是以大写字母开头且后跟小写字母,则它是无效的,因此不匹配。 The regex I made is:我制作的正则表达式是:

/%(?<name>[AZ][az]*)%<(?<product>[A-Za-z]*)>\|(?<quantity>(\d+))\|(?<price>(\d+\.\d*\$))/g

  • This is a match %George%<Croissant>|2|10.3$ (has proper name between the % which has a capital letter start start and followed to the end by lowercase letters), product (the product is between the < and >), a number between both |这是匹配%George%<Croissant>|2|10.3$ (在 % 之间有专有名称,% 以大写字母开头,后面跟着小写字母),产品(产品在 < 和 > 之间) , 两者之间的数字 | and final number (which is the final number and has to have $ in order to be valid)和最终数字(这是最终数字,必须有 $ 才能有效)

  • This does NOT match %InvalidName%<Croissant>|2|10.3$ (because the name is invalid)这与%InvalidName%<Croissant>|2|10.3$不匹配(因为名称无效)

  • This does NOT match %Peter%<Gum>1.3$ (missing number from both |, it has to have 2 numbers.这与%Peter%<Gum>1.3$不匹配(两个 | 中都缺少数字,它必须有 2 个数字。

Tested at regex101 and it matches only %George%<Croissant>|2|10.3$ .在 regex101 上测试,它只匹配%George%<Croissant>|2|10.3$

%Valid%<Valid>valid|10|valid20$ is valid too because it has a Proper name (1 capital letter followed by only lowercase letters, has product which is < Valid >, has number between | 10 | and has a number at the end |valid 20$ %Valid%<Valid>valid|10|valid20$也是有效的,因为它有一个专有名称(1 个大写字母后跟小写字母,产品为 < Valid >,编号介于 | 10 | 之间,编号位于结束|有效20$

To match both patterns, you can match optional word characters \w*\|要匹配这两种模式,您可以匹配可选的单词字符\w*\| before the pipe at group quantity , and match optional word chars without digits y>(\d+)) before group price .在组quantity的 pipe 之前,并匹配组price之前没有数字y>(\d+))的可选单词字符。

To match both prices, you have to make the decimal part optional, because the pattern that you tried expects at least 1 or more digits and a dot using \d+\.\d*要匹配这两个价格,您必须将小数部分设为可选,因为您尝试的模式需要至少 1 个或多个数字和一个使用\d+\.\d*的点

You can also omit the extra capture group in quantity as the digits are already in a named capture group.您还可以在数量上省略额外的捕获组,因为数字已经在命名的捕获组中。

%(?<name>[A-Z][a-z]*)%<(?<product>[A-Za-z]*)>\w*\|(?<quantity>\d+)\|[^\W\d]*(?<price>(\d+(?:\.\d+)?\$))

Explanation解释

  • %(?<name>[AZ][az]*)% Group name between % , match at least an uppercase char AZ and optional lowercase chars az %(?<name>[AZ][az]*)% %的组name ,至少匹配一个大写字符 AZ 和可选的小写字符 az
  • <(?<product>[A-Za-z]*)> Group product Match optional chars A-Za-z <(?<product>[A-Za-z]*)> product组匹配可选字符 A-Za-z
  • \w*\| Match optional word characters, then match |匹配可选的单词字符,然后匹配|
  • (?<quantity>(\d+))
  • \|[^\W\d]* Match optional word characters without the digits (to not overmatch the digits) \|[^\W\d]*匹配不带数字的可选单词字符(不超过数字)
  • (?<price>(\d+(?:\.\d+)?\$)) Group price, with an optional decimal part (?<price>(\d+(?:\.\d+)?\$))组价,小数部分可选

Regex demo正则表达式演示

Try this one试试这个

%(?<name>[A-Z][a-z]*?)%<(?<product>[A-Za-z]*)>[^|]*?\|(?<quantity>\d+)\|[^\d]*(?<price>\d+(?:\.\d+)*\$)

暂无
暂无

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

相关问题 Javascript正则表达式用于不在某些字符之间的所有单词 - Javascript Regex for all words not between certain characters 包含空格和其他字符的单词的Javascript正则表达式 - Javascript regex for words including spaces and other characters 用于完全阻止特殊字符的正则表达式模式,并阻止空格开始和结束字符串,并允许在 javascript 中的单词之间 - regEx pattern for blocking special characters fully, and blocking spaces start and end of the sting and allow in between words in javascript Javascript正则表达式可获取2个字符以及之间的所有字符 - Javascript Regex to get 2 characters and all characters between JavaScript样式特殊字符之间的所有单词 - javascript style all words between special characters JavaScript正则表达式选择两个单词之间的单词 - JavaScript Regex select a word between two words 如何使用 javascript 在文本框中检测这些字符之间的字符和单词? - How to detect characters and words between these characters in a textbox with javascript? JS正则表达式替换两个字符或单词之间的句子 - JS regex replace sentence between two characters or words 使用 Unicode 正则表达式在特殊字符之间查找单词 - Finding words between special characters using Unicode regex 正则表达式避免使用数字和特殊字符,并在单词之间留出空格 - Regex avoid numeric and special characters and allow space between words
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM