2016-09-27 18 views
2

後に私のような文字列を持っていた:私は"$ "後に何かを一致させたい正規表現はドル記号+ホワイトスペース

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

を("$ "含まれていない、ホワイトスペースを注意してください。)。この場合、私は"*ls -ltra*"をobteinします。

私のコードは次のとおりです。私が期待した文字列を取得することで

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

...しかし、グループをキャプチャせずにいることを取得するための別の方法はありますか?

+2

を使用することができますか?これは無料のコーディングサービスではありません – devnull69

+0

あなたの質問は何ですか? –

+0

さて、その最新の編集 - *グループをキャプチャせずにそれを得る別の方法です* - トピックから質問をする、と私は思います。 **いいえ**、それは不可能です。 –

答えて

1

シェルで試み:JavaScriptで

% 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

整合グループ

[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

0123あろう

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"

関連する問題