0

次のように正規表現の検索と置換が必要です。

  • images入力文字列は,videosで終わりますfriends
    • 出力文字列には、一致した接尾辞が含まれています
  • そうしないと
    • 出力文字列には接尾辞が含まれますprofile

入力/出力の例:

  • /john-smith-images->/user-images
  • /john-smith-videos->/user-videos
  • /john-smith->/user-profile

存在する場合、サフィックスをキャプチャするこの正規表現を試しました:

/.+?(images|videos|friends)?$/

私は1 つの正規表現と正規表現のみのソリューションに制限されています。これを mod_rewrite/IIRF/IIS URL 書き換えで使用する必要があります。

4

3 に答える 3

0

これを試してみてください:

text.replace(/[\w-]+(?=(images|videos|friends))/, 'user-').replace(/[\w-]+-(?!(images|videos|friends))\w*/, 'user-profile')
于 2013-03-15T19:29:11.350 に答える
0

次のようにコールバックで String#replace を使用します。

var regexp = /.+?(images|videos|friends|)$/;
function cb ($0, $1) {
   r = $1 ? $1 : 'profile';
   return '/user-' + r;
}
console.log("/john-smith-images".replace(regexp, cb));
console.log("/john-smith-videos".replace(regexp, cb));
console.log("/john-smith".replace(regexp, cb));

出力:

/user-images
/user-videos
/user-profile

ライブデモ: http://ideone.com/MVRcku

于 2013-03-15T19:34:50.127 に答える
0

.replace() を使用する代わりに、条件内で .match() または .test() を使用し、一致しないケースを個別に処理することを検討してください。

于 2013-03-15T19:10:24.820 に答える