我想使用JavaScript(可以使用jQuery)进行一些客户端验证来检查字符串是否与正则表达式匹配:
^([a-z0-9]{5,})$
Run Code Online (Sandbox Code Playgroud)
理想情况下,它将是一个返回true或false的表达式.
我是一个JavaScript新手,确实match()做我需要的东西?它似乎检查字符串的一部分是否匹配正则表达式,而不是整个事物.
我正在尝试创建一个函数,将多个变量与一个整数进行比较,并输出一个由三个字母组成的字符串.我想知道是否有办法将其翻译成Python.所以说:
x = 0
y = 1
z = 3
mylist = []
if x or y or z == 0 :
mylist.append("c")
if x or y or z == 1 :
mylist.append("d")
if x or y or z == 2 :
mylist.append("e")
if x or y or z == 3 :
mylist.append("f")
Run Code Online (Sandbox Code Playgroud)
这将返回一个列表
["c", "d", "f"]
Run Code Online (Sandbox Code Playgroud)
这样的事情可能吗?
在R中,我有一个元素x和一个向量v.我想找到一个元素的第一个索引v,它等于x.我知道这样做的一种方法是:which(x == v)[[1]],但这似乎效率过低.有更直接的方法吗?
对于奖励积分,是否有一个功能,如果x是一个矢量?也就是说,它应该返回一个索引向量,指示xin 的每个元素的位置v.
可能重复:
Javascript StartsWith
我知道我可以这样做^ =看看id是否以某个东西开头,我尝试使用它,但它没有用......基本上,我正在检索网址,我想要设置一个类对于以某种方式开始的路径名元素...
所以,
var pathname = window.location.pathname; //gives me /sub/1/train/yonks/459087
Run Code Online (Sandbox Code Playgroud)
我想确保对于以/ sub/1开头的每个路径,我可以为元素设置一个类......
if(pathname ^= '/sub/1') { //this didn't work...
...
Run Code Online (Sandbox Code Playgroud) 我正在试图弄清楚如何匹配StringRust.
我最初尝试过像这样的匹配,但我发现Rust不能暗中强制转换std::string::String为&str.
fn main() {
let stringthing = String::from("c");
match stringthing {
"a" => println!("0"),
"b" => println!("1"),
"c" => println!("2"),
}
}
Run Code Online (Sandbox Code Playgroud)
这有错误:
error[E0308]: mismatched types
--> src/main.rs:4:9
|
4 | "a" => println!("0"),
| ^^^ expected struct `std::string::String`, found reference
|
= note: expected type `std::string::String`
found type `&'static str`
Run Code Online (Sandbox Code Playgroud)
然后我尝试构造新String对象,因为我找不到将a String转换为a的函数&str.
fn main() {
let stringthing = String::from("c");
match stringthing {
String::from("a") => println!("0"),
String::from("b") => …Run Code Online (Sandbox Code Playgroud) 有没有办法在Javascript中检索正则表达式匹配()的结果字符串中的(起始)字符位置?
String match = "hello";
String text = "0123456789hello0123456789";
int position = getPosition(match, text); // should be 10, is there such a method?
Run Code Online (Sandbox Code Playgroud) 采取以下功能:
def fMatch(s: String) = {
s match {
case "a" => println("It was a")
case _ => println("It was something else")
}
}
Run Code Online (Sandbox Code Playgroud)
这种模式很好地匹配:
scala> fMatch("a")
It was a
scala> fMatch("b")
It was something else
Run Code Online (Sandbox Code Playgroud)
我希望能做的是以下内容:
def mMatch(s: String) = {
val target: String = "a"
s match {
case target => println("It was" + target)
case _ => println("It was something else")
}
}
Run Code Online (Sandbox Code Playgroud)
这会发出以下错误:
fMatch: (s: String)Unit
<console>:12: error: unreachable code
case _ => println("It was something …Run Code Online (Sandbox Code Playgroud) match ×10
regex ×4
javascript ×3
string ×3
python ×2
comparison ×1
if-statement ×1
indexing ×1
java ×1
r ×1
replace ×1
rust ×1
scala ×1
search ×1
vim ×1