[英]Javascript Regex to get 2 characters and all characters between
I have a very basic understanding of Regex and am finding this difficult to achieve. 我对Regex有非常基本的了解,并且发现很难做到这一点。
There is a html select with numerous options like this 有很多这样的html select
<select>
<option value="%001% 25cm Single" label="%001% 25cm Single">%001% 25cm Single</option>
<option value="%002% 25cm Single" label="%002% 25cm Single">%002% 25cm Single</option>
<option value="%003% 25cm Single" label="%003% 25cm Single">%003% 25cm Single</option>
</select>
%001%, %002% etc is going to be used to order the options, the opening % and closing % as well as the content between also needs to be hidden from the frontend. %001%,%002%等将被用于排序的选项,所述开口%和关闭%以及内容之间也需要从前端隐藏。 However, before any of this can be done the %xxx% neededs to be grabbed with regex.
但是,在完成任何此操作之前,必须先使用正则表达式来获取%xxx%。
So what is needed, it seems, is to match the first occurrence for each option of a string that starts with a %, followed by any 3 digits, followed by another %. 因此,需要什么,似乎是符合该用%开头的字符串的每个选项,其次是任何3位数字,紧接着又%的第一次出现。 The numbers between the % are what need to be returned for ordering
%之间的数字是订购时需要返回的内容
There also needs to be a regex rule that matches and returns both the % and the 3 numbers between then so that they can be hidden from the front end using jquery. 此外,还必须匹配,并返回两者%之间,然后将3个数字,使他们能够从使用jQuery前端隐藏一个正则表达式规则。
So far I have 到目前为止,我有
/^%.{3}%/
All help appreciated 所有帮助表示赞赏
要获取%123%的零件,请使用: %[\\d]{3}%
In general, to find all characters up until another stop character, you can use either: 通常,要查找直到另一个终止字符的所有字符,可以使用以下任一方法:
/[^%]+/ // One or more characters which are not a % character
/.+?%/ // One or more non-newline characters, as few as possible, up to a %
So, to find a %
followed by anything up until the next %
, you can use either: 因此,要找到一个
%
然后是直到下一个%
,可以使用以下任一方法:
/%[^%]+%/
/%.+?%/
However, if you know that there must always be exactly three digit characters, then you should use that (more restrictive) regex: 但是,如果您知道必须始终正好包含三个数字字符,则应该使用该(更严格的)正则表达式:
/%\d{3}%/
If you want to preserve any part of the match for separate reference, make it a "capture" by enclosing it in parentheses. 如果要保留匹配的任何部分以供单独参考,请将其括在括号中以使其成为“捕获”。 Each capture will come out as a separate part of the array returned by
String.prototype.match
: 每次捕获将作为
String.prototype.match
返回的数组的单独部分出现:
var marker = /%(\d{3})%/;
var string = "%001% 25cm Single";
var results = string.match(marker);
var number = results && results[1]; // "001"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.