2016-09-27 57 views
2

后什么我有一个像串:(。不包括"$ "请注意空格)正则表达式匹配美元符号加空格

[email protected]:/path/to/somewhere$ ls -ltra 

我想匹配任何后"$ "。在这种情况下,我会反对"*ls -ltra*"

我的代码是:

var res = str.match(/\$ (.*)/); 
console.log(res[1]); 

有了,我得到预期的串......然而,有另一种方式来获得,如果没有捕获组?

+2

你有什么这么远吗?这不是免费的编码服务 – devnull69

+0

你的问题是什么? –

+0

那么,最新的编辑 - *是否有另一种方式来获取,而不捕捉团体* - 我认为,这个问题脱离主题。 **没有**,这是不可能的。 –

回答

1

尝试在一个壳:

% nodejs 
> var file = '[email protected]:/path/to/somewhere$ ls -ltra' 
undefined 
> file 
'[email protected]:/path/to/somewhere$ ls -ltra' 
> var matches = file.match(/\$\s+(.*)/); 
undefined 
> matches 
[ '$ ls -ltra', 
    'ls -ltra', 
    index: 36, 
    input: '[email protected]:/path/to/somewhere$ ls -ltra' ] 
> matches[1] 
'ls -ltra' 
0

在JavaScript中所得匹配组任何的

[email protected]:/path/to/somewhere$ ls -ltra 
[email protected]:/path/to/some$$wh$re$ ls -ltra 
[email protected]:/path/to/somewhere$  ls -ltra 
// and even: 
[email protected]:/path/to/some$$where$ls -ltra 

使用

/(?:\$\s*)([^$]+)$/g 

ls -ltra

https://regex101.com/r/dmVSsL/1←我无法解释更好


您还可以使用.split()

var str = "[email protected]:/path/to/somewhere$ ls -ltra"; 
 
var sfx = str.split(/\$\s+/)[1]; 
 

 
alert(sfx);  // "ls -ltra"

相关问题