简体   繁体   English

java 正则表达式添加斜杠

[英]java regex add trailing slash

I am trying to redirect the urls to add trailing slash我正在尝试重定向网址以添加斜杠

/news -> /news/
/news?param1=value1 -> /news/?param1=value
/news#anchor?param1=value1 -> /news/#anchor?param1=value1

I need to do it through a regex that identifies only the path and add /.我需要通过仅标识路径并添加 / 的正则表达式来执行此操作。 When there are no parameters there is no problem.没有参数时没有问题。

^(/[a-z0–9/_\-]*[^/])$ -> $1/

But when there are parameters I am not able to create the regular expression that separates the path from the parameters.但是当有参数时,我无法创建将路径与参数分开的正则表达式。

Any ideas?, thanks有什么想法吗,谢谢

You shouldn't match the end of the string with $ and there is no need for [^/] at the end either.您不应该将字符串的结尾与$匹配,并且结尾也不需要[^/]

^(/[a-z0–9/_\-]*)

 const regex = new RegExp("^(/[a-z0–9/_\-]*)"); console.log("/news".replace(regex, "$1/")); console.log("/news?param1=value1".replace(regex, "$1/")); console.log("/news#anchor?param1=value1".replace(regex, "$1/"));

The pattern you tried matches only /news because the anchor $ asserts the end of the string.您尝试的模式仅匹配/news因为锚$断言字符串的结尾。

If you omit the anchor, it would also match the ?如果省略锚点,它也会匹配? and # as you use [^/] which matches any char except a forward slash.#当您使用[^/]时,它匹配除正斜杠之外的任何字符。


You could repeat 1 or more times matching a forward slash followed by 1 or more times any char listed in the character class to prevent matching ///您可以重复 1 次或多次匹配正斜杠,然后重复 1 次或多次字符 class 中列出的任何字符,以防止匹配///

In the replacement use the full match and add aa forward slash.在替换中使用完整匹配并添加一个正斜杠。

^(?:/[a-z0-9_-]+)+

Regex demo |正则表达式演示| Java demo Java演示

String regex = "^(?:/[a-z0-9_-]+)+";
String string = "/news\n"
     + "/news?param1=value1\n"
     + "/news#anchor?param1=value1";

Pattern pattern = Pattern.compile(regex, Pattern.MULTILINE);
Matcher matcher = pattern.matcher(string);
String result = matcher.replaceAll("$0/");

System.out.println(result);

Output Output

/news/
/news/?param1=value1
/news/#anchor?param1=value1

Note that in your regex, the hyphen in this part 0–9 is请注意,在您的正则表达式中,这部分0–9中的hyphen

https://www.compart.com/en/unicode/U+2013 instead of https://www.compart.com/en/unicode/U+002D https://www.compart.com/en/unicode/U+2013而不是https://www.compart.com/en/unicode/U+002D

Might be just need to extend the end of string past the parameters.可能只需要将字符串的结尾扩展到参数之外。
To cover both with and without parameters might be:涵盖带参数和不带参数可能是:

^(/[a-z0–9/_-]*(?<!/))([^/]*)$ -> $1/$2 ^(/[a-z0–9/_-]*(?<!/))([^/]*)$ -> $1/$2

see https://regex101.com/r/Iwl23o/2https://regex101.com/r/Iwl23o/2

You can do it as follows:你可以这样做:

public class Main {
    public static void main(final String[] args) {
        String[] arr = { "/news", "/news?param1=value1", "/news#anchor?param1=value1" };
        for (String s : arr) {
            System.out.println(s.replaceFirst("([^\\/\\p{Punct}]+)", "$1/"));
        }
    }
}

Output: Output:

/news/
/news/?param1=value1
/news/#anchor?param1=value1

Explanation of the regex:正则表达式的解释:

You can use a very simple regex like this:你可以像这样使用一个非常简单的正则表达式:

^([/\w]+)

With this replacement string: $1/使用此替换字符串: $1/

Working demo工作演示

在此处输入图像描述

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

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