- CoffeeScript 教程
- CoffeeScript - 主页
- CoffeeScript - 概述
- CoffeeScript - 环境
- CoffeeScript - 命令行实用程序
- CoffeeScript - 语法
- CoffeeScript - 数据类型
- CoffeeScript - 变量
- CoffeeScript - 操作符和别名
- CoffeeScript - 条件
- CoffeeScript - 循环
- CoffeeScript - 解析
- CoffeeScript - 函数
- CoffeeScript 面向对象
- CoffeeScript - 字符串
- CoffeeScript - 数组
- CoffeeScript - 对象
- CoffeeScript - 范围
- CoffeeScript - 展开运算符
- CoffeeScript - 日期
- CoffeeScript - 数学
- CoffeeScript - 异常处理
- CoffeeScript - 正则表达式
- CoffeeScript - 类和继承
- CoffeeScript 高级
- CoffeeScript - Ajax
- CoffeeScript - jQuery
- CoffeeScript - MongoDB
- CoffeeScript - SQLite
- CoffeeScript - 有用资源
- CoffeeScript - 快速指南
- CoffeeScript - 有用资源
- CoffeeScript - 讨论
CoffeeScript 字符串 - search()
说明
此方法接受一个对象形式的正则表达式,并搜索调用字符串中给定的正则表达式。如果匹配发生,它将返回字符串内部正则表达式的索引,如果没有,则返回 -1 值。
语法
下面给出 JavaScript 的 search() 方法的语法。我们可以在 CoffeeScript 代码中使用相同的方法。
string.search(regexp)
示例
以下示例演示了在 CoffeeScript 代码中使用 JavaScript 的 search() 方法。将此代码保存到名为 string_search.coffee 的文件中
regex = /apples/gi
string = "Apples are round, and apples are juicy."
if string.search(regex) == -1
console.log "Does not contain Apples"
else
console.log "Contains Apples"
打开 命令提示符 并如下所示编译 .coffee 文件。
c:\> coffee -c coffee string_search.coffee
编译后,它将提供以下 JavaScript。
// Generated by CoffeeScript 1.10.0
(function() {
var regex, string;
regex = /apples/gi;
string = "Apples are round, and apples are juicy.";
if (string.search(regex) === -1) {
console.log("Does not contain Apples");
} else {
console.log("Contains Apples");
}
}).call(this);
现在,再次打开 命令提示符 并如下所示运行 CoffeeScript 文件。
c:\> coffee string_search.coffee
执行后,CoffeeScript 文件将生成以下输出。
Contains Apples
coffeescript_strings.htm
广告