英文:
Pass a Regular Expression via URL
问题
我有一个类似这样的资源端点:'.../rest/regex/{regex:.+}/matches{value:.+}';
我如何安全地将正则表达式传递到URL?我该如何对其进行编码?
在 JavaScript 中,我尝试这样做:
public matches(regex: string, value: string): ng.IPromise<boolean> {
return this.RestService.httpGet(this.path + this.escapeRegExp(regex) +
'/matches/' + encodeURIComponent(value));
}
escapeRegExp(str: string) {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\$&');
}
但是,当我使用这个正则表达式:TNGSXGIN02BZ0(1|3)[1][8-9][0-5][0-9]\d{4}
时,在服务器端(Java)我会得到这个错误:
java.util.regex.PatternSyntaxException: Unclosed counted closure near index 60 (?i)^TNGSXGIN02BZ0/(1/|3/)/[1/]/[8/-9/]/[0/-5/]/[0/-9/]/d/{4/}$
Java 代码如下:
@Path("regex")
@RunInTransaction
@Produces(MediaType.TEXT_PLAIN)
public final class RegexService {
private static final Logger LOG = Log.getLogger();
@GET
@Path("{regex:.+}/matches/{value:.+}")
public boolean matches(@PathParam("regex") String regex, @PathParam("value") String value) {
LOG.debug("GET Test regex: {} with value: {}", regex, value);
return Pattern.matches("(?i)^" + regex + "$", value);
}
}
更新:
当我在 JavaScript 中这样写:
public matches(regex: string, value: string): ng.IPromise<boolean> {
return this.RestService.httpGet(this.path + encodeURIComponent(regex) +
'/matches/' + encodeURIComponent(value));
}
以及在 Java 中这样写:
@GET
@Path("{regex:.+}/matches/{value:.+}")
public boolean matches(@PathParam("regex") String regex, @PathParam("value") String value) {
LOG.debug("GET Test regex: {} with value: {}", URLDecoder.decode(regex, StandardCharsets.UTF_8), value);
return Pattern.matches("(?i)^" + URLDecoder.decode(regex, StandardCharsets.UTF_8) + "$", value);
}
我仍然会得到一个异常:
(?i)^TNGSXGIN02BZ0/(1/|3/)/[1/]/[8/-9/]/[0/-5/]/[0/-9/]/d/{4/}$```
<details>
<summary>英文:</summary>
I have resource endpoint like this: '.../rest/regex/{regex:.+}/matches{value:.+}';
How can i safely pass the regex in the url? How do I encode it?
In javascript I tried to do:
```javascript
public matches(regex: string, value: string): ng.IPromise<boolean> {
return this.RestService.httpGet(this.path + this.escapeRegExp(regex) +
'/matches/' + encodeURIComponent(value));
}
escapeRegExp(str: string) {
return str.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, '\$&');
}
But when I use this regex: TNGSXGIN02BZ0(1|3)[1][8-9][0-5][0-9]\d{4}
then server-side (java) i get this error:
java.util.regex.PatternSyntaxException: Unclosed counted closure near index 60
(?i)^TNGSXGIN02BZ0/(1/|3/)/[1/]/[8/-9/]/[0/-5/]/[0/-9/]/d/{4/}$
Java code looks like this:
@Path("regex")
@RunInTransaction
@Produces(MediaType.TEXT_PLAIN)
public final class RegexService {
private static final Logger LOG = Log.getLogger();
@GET
@Path("{regex:.+}/matches/{value:.+}")
public boolean matches(@PathParam("regex") String regex, @PathParam("value") String value) {
LOG.debug("GET Test regex: {} with value: {}", regex, value);
return Pattern.matches("(?i)^" + regex + "$", value);
}
}
UPDATE:
When i have this in javascript:
public matches(regex: string, value: string): ng.IPromise<boolean> {
return this.RestService.httpGet(this.path + encodeURIComponent(regex) +
'/matches/' + encodeURIComponent(value));
}
And this in Java:
@GET
@Path("{regex:.+}/matches/{value:.+}")
public boolean matches(@PathParam("regex") String regex, @PathParam("value") String value) {
LOG.debug("GET Test regex: {} with value: {}", URLDecoder.decode(regex, StandardCharsets.UTF_8), value);
return Pattern.matches("(?i)^" + URLDecoder.decode(regex, StandardCharsets.UTF_8) + "$", value);
}
I still get an exception:
>java.util.regex.PatternSyntaxException: Unclosed counted closure near index 60
(?i)^TNGSXGIN02BZ0/(1/|3/)/[1/]/[8/-9/]/[0/-5/]/[0/-9/]/d/{4/}$
答案1
得分: 2
你需要对你的正则表达式进行 URL 编码和解码。
对于 JavaScript 编码:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
对于 Java 中的解码:https://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html
英文:
You need to URL encode & decode your regex.
For Javascript encoding: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/encodeURIComponent
For decoding in Java: https://docs.oracle.com/javase/7/docs/api/java/net/URLDecoder.html
答案2
得分: -1
可以还使用 String 构造函数:
let regex = new RegExp(".*", "g");
let urlRegex = encodeURIComponent(String(regex));
console.log(urlRegex);
let regexresult = new RegExp(decodeURIComponent(urlRegex));
console.log(decodeURIComponent(urlRegex));
英文:
You can also make use of the String constructor:
let regex = new RegExp(".*", "g");
let urlRegex = encodeURIComponent(String(regex));
console.log(urlRegex);
let regexresult = new RegExp(decodeURIComponent(urlRegex));
console.log(decodeURIComponent(urlRegex));
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论