如何将这个PHP正则表达式转换为JS [关闭]

I have the following RegEx that works in PHP, but I can't figure out how to convert it so that it works in JS.

$string='test/again/'
preg_replace('/[^\/]+\/$/', '', $string)

this takes this: test/again/

and makes it into: test/

Thanks for your help.

This replace should do the trick

var string = 'test/again/'
string = string.replace(/[^\/]+\/$/,'');

/[^\/]+\/$/ should do the trick.

Edit: I see that you edited your post, and your RegExp now just looks the same. What's your question again?

Use .replace():

Demo

var string = 'test/again/';
var result = string.replace(/[^\/]+\/$/, '');