- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我在 Javascript 中使用 startswith
reg exp
if ((words).match("^" + string))
但是如果我输入像 , ] [\/
这样的字符,Javascript 会抛出异常。有什么想法吗?
最佳答案
如果您使用正则表达式进行匹配,则必须确保传递有效的 Regular Expression匹配()。检查list of special characters以确保您没有传递无效的正则表达式。以下字符应始终进行转义(在其前面放置一个\):[\^$.|?*+()
更好的解决方案是像这样使用 substr():
if( str === words.substr( 0, str.length ) ) {
// match
}
或者使用 indexOf 的解决方案是(看起来更干净一些):
if( 0 === words.indexOf( str ) ) {
// match
}
接下来,您可以将 startsWith() 方法添加到包含上述两种解决方案中的任何一种的字符串原型(prototype),以提高用法的可读性:
String.prototype.startsWith = function(str) {
return ( str === this.substr( 0, str.length ) );
}
添加到原型(prototype)后,您可以像这样使用它:
words.startsWith( "word" );
关于javascript - javascript错误中的startwith,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1409476/
我试图匹配文件前缀是否与我的对象匹配,但我遇到了类转换异常 for (int i=0;i ,但在运行时它实际上包含一个 java.lang.Boolean 。由于泛型的实现方式(称为 "erasur
我有 Observable 发出一些 String: List list = // size this list is 5 Observable o1 = Observable.fromArray(l
我尝试运行 jquery 但没有成功: $(function () { var pathname = window.location.pathname; var a = path
我正在尝试使用 startWith 过滤器,但它没有正确过滤数据,就像 P 的情况一样。让我知道我在这里做错了什么。 Controller 代码 - var app = angular.module(
我对Kotlin不满意(尽管我在Java等其他语言方面有很多经验)。我试图用Kotlin编写一些代码,但不明白为什么以下代码无法编译。 private fun test(inpStr : String
自从更新到 RXJS 第 6 版后,我的 WebStorm 编辑器一直在提示 startWith() 的某些用法。该运算符被标记为已弃用。 您可以在源代码中看到这些方法被标记为已弃用: Link to
我是 Observables 和响应式(Reactive)编程 rxjs 的新手,我已将 redux-observable 库添加到我的 React/redux 应用程序中以开始使用它。 我为登录过程
以下代码仅在 observable2 完成后才从 observable1 发出项目。 observable1.startWith(observable2) .subscribe(
我有一个使用 startWith 运算符和 debounceTime 的流。我希望第一个值跳过 debounceTime 并立即开始。我该怎么做? control.valueChanges .
我正在使用 Typescript 学习 Angular 5。我正在尝试实现一个 Angular Material 自动完成功能,我发现以下代码对我来说非常晦涩: this.filteredStates
我有一个关于多播 observables 和我注意到的意外(对我而言)行为的问题。 const a = Observable.fromEvent(someDom, 'click') .map(e
我有一个 rx observable,我需要添加一个特定的项目,但前提是 observable 至少发出一个项目。 我找遍了所有的运营商,但找不到合适的。最接近的是 StartWith但根据图表,它会
请有人告诉我为什么我的 startsWith() 方法是红色的。我对 Selenium 和 Java 很陌生,非常感谢任何帮助。我使用的是 IntelliJ IDEA 社区版、Java 和 Windo
我的应用程序中有许多用于网络请求的 Observable。由于有太多相同之处,我对它们应用了 Observable 转换: /** * Creates a transformer that appl
我有以下代码。这是基于使用临时容器来选择特定项目,然后将它们添加到列表的末尾。 var allRoles = roles.Table .AsEnumerable().Select(p =
我正在使用另一个 Observable 的输入来过滤一个 Observable - 过滤的输入来自用户。 过滤是通过 RxJS 运算符combineLatest完成的。使用这个意味着当订阅这个流时,在
如果我希望订阅者最初至少获得 X,我可以对现有的 Observable 使用 startWith( X ) 吗: streamFromLibrary.startWith( X ).subscribe(
我有两个列表。两者包含相同的值: QStringList filePaths; filePaths.append("C:/backup"); filePaths.append("C:/downl
是否有任何预定义函数可以使用 Rust 的迭代器开始迭代自定义元素? 最佳答案 iteration with a custom element 如果您只有一个元素,请使用 iter::once . 如
有人知道“startWith”函数变成了什么吗? 我尝试使用一本适用于 4.x 版本的书来学习 reactivecocoa。该示例使用 startWith 为 datePicker 控件强制赋值。 最
我是一名优秀的程序员,十分优秀!