Javascript正则表达式获取子字符串,不包括模式?
我还是个初学者:) 我需要得到一个子字符串,忽略Javascript正则表达式获取子字符串,不包括模式?,javascript,regex,substring,Javascript,Regex,Substring,我还是个初学者:) 我需要得到一个子字符串,忽略[]中的最后一部分(包括括号[]),即忽略最后的[内部某物]部分 注意-字符串中可能有其他单一出现的[。它们应该出现在结果中 示例 表格的输入— 1 checked arranged [1678] 期望输出- 1 checked arranged 我试过这个 var item = "1 checked arranged [1678]"; var parsed = item.match(/([a-zA-Z0-9\s]+)([(\[d+\])]+
[]
中的最后一部分(包括括号[]),即忽略最后的[内部某物]
部分
注意-字符串中可能有其他单一出现的[
。它们应该出现在结果中
示例
表格的输入—
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);
var item=“1已检查安排[1678]”;
var parsed=item.match(/([a-zA-Z0-9\s]+)([(\[d+\])]+)$/);
|||
警报(已解析);
我的意思是:
第1节多次出现单词(包含文字和编号),后跟空格
第2节忽略最后的模式[某物]
但我得到的是1678]、1678、]
,我不确定它会朝哪个方向发展
谢谢在这里您可以找到如何删除方括号内的内容。这将使您与其他人一起离开。:)
在这里,您可以找到如何删除方括号内的内容。这将使您与其他人一起离开。:) 你可以这样做
var s = "1 checked arranged [1678]";
var a = s.indexOf('[');
var b = s.substring(0,a);
alert(b);
这个s.indexOf('[');
检查字符串中第一个[
出现的位置
这个s.substring(0,a);
从开始到第一个[
切掉字符串
当然,这假设字符串的格式总是相似的,您可以这样做
var s = "1 checked arranged [1678]";
var a = s.indexOf('[');
var b = s.substring(0,a);
alert(b);
这个s.indexOf('[');
检查字符串中第一个[
出现的位置
这个s.substring(0,a);
从开始到第一个[
切掉字符串
当然,这假设字符串总是采用类似的格式如果您只想在最后去掉该[],请尝试此方法
var parsed = item.replace(/\s*\[[^\]]*\]$/,"")
如果你最终只想摆脱那个[],试试这个
var parsed = item.replace(/\s*\[[^\]]*\]$/,"")
那工作按预期进行吗
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]);
我使用的正则表达式使用排除字符串中不需要的部分。括号中的数字必须是字符串的一部分,匹配才能成功,但不会在结果中返回
我使用的正则表达式使用a来排除字符串中不需要的部分。括号中的数字必须是字符串的一部分,匹配才能成功,但它不会在结果中返回。使用转义括号和非捕获括号:
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
使用转义括号和非捕获括号:
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
好的,这是你表达式中的问题
([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+\])$
看,完整的字符串是匹配的,第一组中的第1节和第2组中的第2节
当您现在用组1的内容替换整个内容时,您就完成了。好的,这是您表达式中的问题
([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+\])$
看,完整的字符串是匹配的,第一组中的第1节和第2组中的第2节
当你现在用第1组的内容替换整个内容时,你就完成了。但是字符串中可能有其他单一出现的
[
。很抱歉,我在问题中遗漏了这一点。没问题@Sandepan Nath。如果测试总是在[
那么这仍然有效。但是字符串中可能有其他单一的[
出现。很抱歉,我没有在问题中提到这一点。没问题@Sandepan Nath。如果测试总是在第一次出现[
之前进行,那么这仍然有效。