Javascript正则表达式获取子字符串,排除模式?

发布于 2024-11-27 20:10:28 字数 763 浏览 1 评论 0原文

我还是一个初学者:)

我需要获取一个子字符串,忽略 [] 内的最后一部分(包括括号 []),即忽略 [something inside] 部分到底。

注意 - 字符串中可能还会出现其他单一的 [。它们应该出现在结果中。

示例

表单的输入 -

1 checked arranged [1678]

所需的输出 -

1 checked arranged

我尝试使用此

var item = "1 checked arranged [1678]";

var parsed = item.match(/([a-zA-Z0-9\s]+)([(\[d+\])]+)$/);
                          |<-section 1  ->|<-section 2->|

alert(parsed);

我试图表示以下内容 -

第 1 节 - 多次出现单词(包含文字和编号),后跟空格

第 2 部分 - 忽略最后的模式 [something]。

但我收到 1678],1678,] 并且我不确定它会朝哪个方向发展。

谢谢

I am still a beginner :)

I need to get a substring ignoring the last section inside [] (including the brackets []), i.e. ignore the [something inside] section in the end.

Note - There could be other single occurances of [ in the string. And they should appear in the result.

Example

Input of the form -

1 checked arranged [1678]

Desired output -

1 checked arranged

I tried with this

var item = "1 checked arranged [1678]";

var parsed = item.match(/([a-zA-Z0-9\s]+)([(\[d+\])]+)$/);
                          |<-section 1  ->|<-section 2->|

alert(parsed);

I tried to mean the following -

section 1 - multiple occurrences of words (containing literals and nos.) followed by spaces

section 2 - ignore the pattern [something] in the end.

But I am getting 1678],1678,] and I am not sure which way it is going.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

北渚 2024-12-04 20:10:28

好的,这是您的表达式中的问题

([a-zA-Z0-9\s]+)([(\[d+\])]+)$

问题仅出现在最后一部分,

([(\[d+\])]+)$
 ^        ^
 here are you creating a character class, 
 what you don't want because everything inside will be matched literally.

((\[d+\])+)$
 ^      ^^
here you create a capturing group and repeat this at least once ==> not needed

(\[d+\])$
   ^
  here you want to match digits but forgot to escape

这使我们可以

([a-zA-Z0-9\s]+)(\[\d+\])$

这里在 Regexr 上查看它,完整的字符串被匹配,捕获组 1 中的第 1 部分和组 2 中的第 2 部分。

现在,当您将整个字符串替换为组 1 的内容时,您就完成了。

OK here is the problem in your expression

([a-zA-Z0-9\s]+)([(\[d+\])]+)$

The Problem is only in the last part

([(\[d+\])]+)$
 ^        ^
 here are you creating a character class, 
 what you don't want because everything inside will be matched literally.

((\[d+\])+)$
 ^      ^^
here you create a capturing group and repeat this at least once ==> not needed

(\[d+\])$
   ^
  here you want to match digits but forgot to escape

That brings us to

([a-zA-Z0-9\s]+)(\[\d+\])$

See it here on Regexr, the complete string is matched, the section 1 in capturing group 1 and section 2 in group 2.

When you now replace the whole thing with the content of group 1 you are done.

得不到的就毁灭 2024-12-04 20:10:28

你可以这样做

var s = "1 checked arranged [1678]";

var a = s.indexOf('[');

var b = s.substring(0,a);

alert(b);

http://jsfiddle.net/jasongennaro/ZQe6Y/1/

这个< code>s.indexOf('['); 检查第一个 [ 出现在字符串中的位置。

s.substring(0,a); 会从开头到第一个 [ 截取字符串。

当然,这假设字符串始终采用相似的格式

You could do this

var s = "1 checked arranged [1678]";

var a = s.indexOf('[');

var b = s.substring(0,a);

alert(b);

http://jsfiddle.net/jasongennaro/ZQe6Y/1/

This s.indexOf('['); checks for where the first [ appears in the string.

This s.substring(0,a); chops the string, from the beginning to the first [.

Of course, this assumes the string is always in a similar format

有深☉意 2024-12-04 20:10:28
var item = '1 check arranged [1678]',
    matches = item.match(/(.*)(?=\[\d+\])/));

alert(matches[1]);

我使用的正则表达式利用 正向预测 来排除字符串中不需要的部分。括号中的数字必须是字符串的一部分才能匹配成功,但不会在结果中返回。

var item = '1 check arranged [1678]',
    matches = item.match(/(.*)(?=\[\d+\])/));

alert(matches[1]);

The regular expression I used makes use of a positive lookahead to exclude the undesired portion of the string. The bracketed number must be a part of the string for the match to succeed, but it will not be returned in the results.

浅笑依然 2024-12-04 20:10:28

在这里您可以找到如何删除方括号内的内容。剩下的就交给你了。 :)
正则表达式:删除方括号内容

Here you can find how to delete stuff inside square brackets. This will leave you with the rest. :)
Regex: delete contents of square brackets

戴着白色围巾的女孩 2024-12-04 20:10:28

如果你只想最后摆脱那个 [] 试试这个

var parsed = item.replace(/\s*\[[^\]]*\]$/,"")

try this if you only want to get rid of that [] in the end

var parsed = item.replace(/\s*\[[^\]]*\]$/,"")
著墨染雨君画夕 2024-12-04 20:10:28
var item = "1 checked arranged [1678]";
var parsed = item.replace(/\s\[.*/,"");
alert(parsed);

那工作如愿吗?

var item = "1 checked arranged [1678]";
var parsed = item.replace(/\s\[.*/,"");
alert(parsed);

That work as desired?

故事与诗 2024-12-04 20:10:28

使用转义括号和非捕获括号:

var item = "1 checked arranged [1678]";
var parsed = item.match(/([\w\s]+)(?:\s+\[\d+\])$/);
alert(parsed[1]); //"1 checked arranged"

正则表达式的解释:

([\w\s]+)    //Match alphanumeric characters and spaces
(?:          //Start of non-capturing parentheses
\s*          //Match leading whitespace if present, and remove it
\[           //Bracket literal
\d+          //One or more digits
\]           //Bracket literal
)            //End of non-capturing parentheses
$            //End of string

Use escaped brackets and non-capturing parentheses:

var item = "1 checked arranged [1678]";
var parsed = item.match(/([\w\s]+)(?:\s+\[\d+\])$/);
alert(parsed[1]); //"1 checked arranged"

Explanation of regex:

([\w\s]+)    //Match alphanumeric characters and spaces
(?:          //Start of non-capturing parentheses
\s*          //Match leading whitespace if present, and remove it
\[           //Bracket literal
\d+          //One or more digits
\]           //Bracket literal
)            //End of non-capturing parentheses
$            //End of string
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文