2 人;在 Javascript 中 - 检测特殊字符并将所有 * 替换为 %

发布于 2025-01-04 03:17:28 字数 485 浏览 2 评论 0原文

问题 1:我尝试了以下方法来检测字符(大写和小写)、数字和 *,但收到错误消息,指出它不是有效的正则表达式。

var pattern = '?[a-zA-Z0-9*][a-zA-Z0-9*]*';

问题 2:如何使用正则表达式将字符串中所有 * 字符替换为 % 字符?我是一个正则表达式菜鸟,到目前为止我尝试过的一切都在轰炸...... (已回答)

编辑 1 & 2:澄清我的疯狂。

编辑3:想要提供我的最终解决方案:

这就是我最终得到的模式匹配结果:

var pattern = "^[a-zA-Z0-9*]*$";
var regEx = new RegExp(pattern);
if(regEx.test(searchText) === true) {
    return true;
}

下面选定的答案按照我最初的要求进行了替换。感谢大家的回答和帮助。

Question 1: I've tried the following to detect characters (uppercase and lowercase), numbers and * but I am getting an error that it's not a valid regular expression.

var pattern = '?[a-zA-Z0-9*][a-zA-Z0-9*]*';

Question 2: How can I replace all instances of a * character with a % character in a string with a regular expression? I'm a regex noob and everything I've tried so far keeps bombing...
(Answered)

Edit 1 & 2: Clarification of my lunacy.

Edit 3: Wanted to provide my final solution:

This is what I ended up with for my pattern matching:

var pattern = "^[a-zA-Z0-9*]*$";
var regEx = new RegExp(pattern);
if(regEx.test(searchText) === true) {
    return true;
}

The selected answer below does the replacement as I initially asked for. Thank you all for your answers and help.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

赠意 2025-01-11 03:17:28

这个问题让我感到害怕,因为它表明您正在创建一个 SQL 注入 漏洞,但答案是:

s = 'foo*bar*baz';
s = s.replace(/\*/g, '%');
// now s == 'foo%bar%baz'

This question frightens me, because it suggests that you're creating a SQL injection vulnerability, but the answer is:

s = 'foo*bar*baz';
s = s.replace(/\*/g, '%');
// now s == 'foo%bar%baz'
皓月长歌 2025-01-11 03:17:28
var newString = "foo*bar*baz".replace(/\*/g, "%");

g 正则表达式标志表示“全局”或“全部”。

var newString = "foo*bar*baz".replace(/\*/g, "%");

The g regex flag means "globally", or "all of 'em".

冬天旳寂寞 2025-01-11 03:17:28

回答“问题 1”部分

首先,如果您想定义一个正则表达式模式以在其他地方使用,请使用斜杠 / 而不是引号 " 例如,

/?[a-zA-Z0-9*][a-zA-Z0-9*]*/

这不是一个有效的正则表达式,因为“可选”? 运算符需要在其左侧进行操作,就像 * 一样删除它确实会给你一个有效的正则表达式。

var pattern = /[a-zA-Z0-9*][a-zA-Z0-9*]*/;

改进 - 由于您的两个字符类 [a-zA-Z0-9*] 完全相同,因此您不必重复它们,只需使用 + 运算符而不是 *

var pattern = /[a-zA-Z0-9*]+/;

和列表 a-zA-Z 只需通过附加运算符 i 来忽略大小写

var pattern = /[A-Z0-9*]+/i;

最后,如果您想测试输入完全匹配,您需要在开始和结束处锚定模式,否则如果模式出现在输入中的任何位置,.match() 将找到该模式。

var pattern = /^[A-Z0-9*]+$/i;
var input = "Foo*Bar*baz";
console.log("Got: " + input.match(pattern);

Answering the "Question 1" part

First, if you want to define a regex pattern to use elsewhere, it is done with slashes / not quotes " e.g.

/?[a-zA-Z0-9*][a-zA-Z0-9*]*/

This is not a valid regular expression, because the "optional" ? operator needs something to its left to operate on, just like * does. Removing that does give you a valid regex.

var pattern = /[a-zA-Z0-9*][a-zA-Z0-9*]*/;

Improvements - since your two character classes [a-zA-Z0-9*] are exactly the same, you don't have to repeat them, just use the + operator instead of *

var pattern = /[a-zA-Z0-9*]+/;

and rather than list a-zA-Z just ignore case by appending the operator i

var pattern = /[A-Z0-9*]+/i;

Finally, if you want to test the input exactly matches, you need to anchor the pattern at both the start and end, otherwise .match() will find the pattern if it appears anywhere in the input.

var pattern = /^[A-Z0-9*]+$/i;
var input = "Foo*Bar*baz";
console.log("Got: " + input.match(pattern);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文