简体   繁体   English

如何使用JavaScript正则表达式进行匹配?

[英]How to match that using a JavaScript regex?

This is my code: 这是我的代码:

var name = 'somename';
var pass = '123somen456';
var regex = new RegExp('.*' + pass + '.*', 'i');

alert(name.match(regex));

The regex just wont match, what I dont understand. 正则表达式不会匹配,我不明白。 Whats wrong here? 怎么了 I want to have a match as soon as any part of name is contained in pass, as long as that match is at least 4 chars long. 只要名称中的任何部分包含在通行证中,只要该匹配至少有4个字符,我就想进行一次匹配。 Example: 例:

som --> no match 索姆->不匹配
some --> match 一些->匹配

Thanks! 谢谢!

This regex requires that there are any amount of any character, then 123somen456 , and then any amount of any character. 此正则表达式要求包含任意数量的任何字符,然后是123somen456 ,然后包含任意数量的任何字符。 name.match(regex) will not return anything because name does not contain the string 123somen456 . name.match(regex)将不返回任何内容,因为name不包含字符串123somen456

To test regular expressions, I recommend using http://regexpal.com/ . 要测试正则表达式,建议使用http://regexpal.com/

It sounds, you may need to apply some algorithms like this or this . 听起来,您可能需要应用某些算法,例如thisthis

If it is possible using regex in javascript, I'm interested to know. 如果有可能在javascript中使用正则表达式,我很想知道。

sorry buddy, I have no option to comment. 抱歉,哥们,我没有选择发表评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM