【发布时间】:2023-03-27 17:39:01
【问题描述】:
【问题讨论】:
标签: javascript
【问题讨论】:
标签: javascript
您可以使用regular expressions in Javascript 对字符串进行模式匹配。
例如:
var s = "hello world!";
if (s.match(/hello.*/)) {
// do something
}
match() 测试很像 SQL 中的WHERE s LIKE 'hello%'。
【讨论】:
test(顺便说一下,应该更快),因为不需要实际的匹配结果。
/regex/.test(s),除非你想对匹配做点什么-stackoverflow.com/a/10940138/1128978
没有。
您要使用:.indexOf("foo"),然后检查索引。如果 >= 0,则包含该字符串。
【讨论】:
myString.toLowerCase().indexOf("foo")
toString()。 myInteger.toString().indexOf(value) > -1.
使用字符串对象匹配方法:
// Match a string that ends with abc, similar to LIKE '%abc'
if (theString.match(/^.*abc$/))
{
/*Match found */
}
// Match a string that starts with abc, similar to LIKE 'abc%'
if (theString.match(/^abc.*$/))
{
/*Match found */
}
【讨论】:
您可以检查String.match() 或String.indexOf() 方法。
【讨论】:
不,没有,但您可以查看indexOf 作为开发自己的起点,和/或查看regular expressions。熟悉JavaScript string functions 是个好主意。
编辑:这已经回答过:
【讨论】:
【讨论】: