简体   繁体   English

正则表达式从某个字符开始提取字符串而不包含该字符本身

[英]Regular expression to extract the string starting from a certain character without that character itself

Suppose I have the following js code: 假设我有以下js代码:

r='d|daa|dd';  
reg = /\|.*/;  
result = reg.exec(r);

This will return |daa|dd , but what I want is daa|dd . 这将返回|daa|dd ,但我想要的是daa|dd

What should the regex be? 正则表达式应该是什么? Thanks 谢谢

Grouping is the way to go: 分组是要走的路:

reg = /\|(.*)/;

It will search for string starting with |, but will capture the group (enclosed in brackets) separately for further use. 它将搜索以|开头的字符串,但会分别捕获该组(括在括号中)以供进一步使用。

r='d|daa|dd';
reg = /\|(.*)/;
result = reg.exec(r);

result[1] is then populated with what's in the capture group. 然后将result[1]填充到捕获组中的内容。

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

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