简体   繁体   English

Javascript正则表达式获取子字符串,不包括模式?

[英]Javascript regex to get substring, excluding a pattern?

I am still a beginner :) 我仍然是初学者:)

I need to get a substring ignoring the last section inside [] (including the brackets []), ie ignore the [something inside] section in the end. 我需要得到一个忽略[]最后一部分(包括方括号[])的子字符串,即忽略最后的[something inside]部分。

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 第1节 -多次出现单词(包含文字和数字),后跟空格

section 2 - ignore the pattern [something] in the end. 第2节 -最终忽略模式。

But I am getting 1678],1678,] and I am not sure which way it is going. 但是我得到的是1678],1678,] ,但我不确定它的发展方向。

Thanks 谢谢

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. 在Regexr上看到它,完整的字符串匹配,捕获组1中的第1部分和组2中的第2部分。

When you now replace the whole thing with the content of group 1 you are done. 现在,将整个内容替换为第1组的内容时,您就完成了。

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/ http://jsfiddle.net/jasongennaro/ZQe6Y/1/

This s.indexOf('['); 这个s.indexOf('['); checks for where the first [ appears in the string. 检查第一个[在字符串中出现的位置。

This s.substring(0,a); 这个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 当然,这假设字符串始终采用相似的格式

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. 括号中的数字必须是字符串的一部分,才能成功进行匹配,但不会在结果中返回该数字。

Here you can find how to delete stuff inside square brackets. 在这里,您可以找到如何删除方括号内的内容。 This will leave you with the rest. 这将使您剩下的一切。 :) Regex: delete contents of square brackets :)正则表达式:删除方括号中的内容

如果最终只想摆脱该[],请尝试此操作

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

That work as desired? 想要的工作?

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

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

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