如何使用replace()替换Javascript中的星号? [英] How do I replace an asterisk in Javascript using replace()?

查看:1656
本文介绍了如何使用replace()替换Javascript中的星号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用JQuery,我从MySQL数据库中的某些数据的本质查询框中提取值。知道大多数用户将使用'*'(星号)作为通配符值并且MySQL使用'%'字符,我想将任何星号转换为'%'。

Using JQuery, I'm extracting the value from what is essentially a query box for some data in a MySQL database. Knowing that most users will use an '*' (asterisk) as a wildcard value and that MySQL uses the '%' character, I want to convert any asterisks to '%'.

通常情况下,这就像使用

Normally, this would just be as simple as using

queryString = inputText.replace('*', '%');

但是我没有成功。 Javascript的 replace()函数只替换第一个匹配项,因此它不会替换最后一个星号,如 * Foo *

but I've had little success with that. Javascript's replace() function only replaces the first occurrence, so it's not replacing the last asterisk in something like *Foo*

我尝试的第二个选项是使用正则表达式,它也不起作用:

The second option I tried was using a regular expression, which also didn't work:

queryString = inputText.replace(/\x2a/g, '%');

如何用'%'(百分号)替换'*'(星号)?我想象有一个非常简单的正则表达式,或者我忽视的东西。

How can I replace the '*' (asterisks) with a '%' (percent sign)? I'd imagine there's a really simple regular expression, or something I'm overlooking.

推荐答案

尝试:

queryString = inputText.replace(/\*/g, '%');

这篇关于如何使用replace()替换Javascript中的星号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆