针对所有情况进行切换
switch for all cases
本文关键字:情况 更新时间:2023-09-26
我想检查输入的任何字符串是否属于类名词。
var answer = prompt("enter sentence").toLowerCase();
function Noun(name) {
this.name = name;
}
spoon = new object("noun");
我可以找到任何子字符串(如 wwwspoonwww 和属于任何类)之间的匹配吗?
var analysis = function(string) {
switch (true) {
case /any string/.test(string):
if (?){
console.log("noun");
}
else {
console.log("not noun")
};
break;
}
};
analysis(answer);
您想查找是否存在与之相关的名词类 字符串?- @plalx
是的,我愿意 - @pi1yau1u
在这种情况下,您可以使用映射来存储所有新创建的实例,并实现一个 API,允许您按名称检索它们或检索整个列表。我还实现了一个 findIn
实例方法,该方法将在指定字符串中返回该名词的索引,如果未找到,则返回 -1。
演示
var Noun = (function (map) {
function Noun(name) {
name = name.toLowerCase();
var instance = map[name];
//if there's already a Noun instanciated with that name, we return it
if (instance) return instance;
this.name = name;
//store the newly created noun instance
map[name] = this;
}
Noun.prototype.findIn = function (s) {
return s.indexOf(this.name);
};
Noun.getByName = function (name) {
return map[name.toLowerCase()];
};
Noun.list = function () {
var m = map, //faster access
list = [],
k;
for (k in m) list.push(m[k]);
return list;
};
return Noun;
})({});
new Noun('table');
new Noun('apple');
//retrieve a noun by name
console.log('retrieved by name :', Noun.getByName('table'));
//check if a string contains some noun (without word boundaries)
var s = 'I eat an apple on the couch.';
Noun.list().forEach(function (noun) {
if (noun.findIn(s) !== -1) console.log('found in string: ', noun);
});
要检查某个字符串是否在另一个字符串中,您可以使用str.indexOf(string);
在这种情况下,您不需要使用该switch
。但是"对于所有情况",您将使用default:
相关文章:
- 如何在未直接触发的情况下停止事件
- 在不使用JQuery的情况下隐藏DOM中的选定元素
- 在不使用jquery的情况下查找页面中的所有锚点并附加函数
- 在不打开聊天屏幕的情况下制作Zopim-ding代理
- 在不阻止默认行为的情况下检测IE10中的缩放
- Array.length似乎不起作用;console.log则显示其他情况
- 如何在不传递此信息的情况下查找被调用的元素
- 如何在不刷新页面的情况下更新显示框
- 有没有一种方法可以在控制台关闭的情况下让console.log()在IE中记录消息
- JavaScript-在手机上不工作的情况下,在外部单击时隐藏元素
- 在我的情况下,如何进行http请求
- 在不知道深度或父属性的情况下从对象中删除属性
- 在不破坏未定义函数的情况下,对多个视图使用单个js文件
- ROR:如何在不重新加载浏览器的情况下从控制器获取参数
- 如何在不影响其他元素的情况下扩展DIV
- 如何在内联依赖项并将图像转换为dataURI的情况下完全提取网页
- 在不移动内部文本的情况下缩放元素的效果
- 在不设置协议的情况下,使用javascript/jquery更改iframe-src
- 默认情况下折叠和展开嵌套列表
- 如何在不单击javascript中的按钮的情况下触发事件