简体   繁体   English

在字符串Javascript中查找单词

[英]Find word in string Javascript

I have a set of strings: 我有一组字符串:

s1 = 'user-1 color-class';
s2 = 'text-class user-1 color-class";
s3 = 'text-class not-user-1 color-class";
s4 = 'not-user-1 color-class";
s5 = 'not-user-1 user-1 color-class";

I want to return true or false for all the strings that contain the word starting with player- . 对于包含以player-开头的单词的所有字符串,我想返回true或false。 That is, not as part of another word, but starting with. 也就是说,不是作为另一个词的一部分,而是从头开始。

So for the above 所以对于上面

s1 = 'user-1 color-class';  <-- TRUE
s2 = 'text-class user-1 color-class";  <-- TRUE
s3 = 'text-class not-user-1 color-class";  <-- FALSE
s4 = 'not-user-1 color-class";  <-- FALSE
s5 = 'not-user-1 user-1 color-class";  <-- TRUE

So for example: 因此,例如:

if (hasUserClass(string)) {           <-- boolean

Additionally, how do I get the user-X value from the class string? 另外,如何从类字符串中获取user-X值?

userClass = getUserClass(string);     <-- `user-3`

Thanks 谢谢

This is what I have done so far: 到目前为止,这是我所做的:

function getUserClass(element)
{
    if (! element) {
        return false;
    }

    var classes = $(element).attr('class');

    if (classes.search('user-') == -1) {
        return false;
    }

    return classes.match(/player\-\d+/gi)[0];
}


function userHasClass(element)
{
    if (! element) {
        return false;
    }

    var userClass = getUserClass(element);

    return (userClass == active_user.class);
}

But this does not take into account is must be the start of the word matched. 但这并没有考虑到必须是单词匹配的开始。

Here's an ES6 one-liner. 这是ES6一线纸。

 s1 = 'user-1 color-class'; //<-- TRUE s2 = 'text-class user-1 color-class'; //<-- TRUE s3 = 'text-class not-user-1 color-class'; //<-- FALSE s4 = 'not-user-1 color-class'; //<-- FALSE s5 = 'not-user-1 user-1 color-class'; //<-- TRUE const myTest = words => words.split(" ").some( str => str.startsWith("user-") ) console.log(myTest(s1), myTest(s2) ,myTest(s3), myTest(s4), myTest(s5)) 

https://www.w3schools.com/jsref/jsref_indexof.asp https://www.w3schools.com/jsref/jsref_indexof.asp

Take a look to this. 看看这个。

if you use 如果您使用

var str = "Hello world, welcome to the universe.";
var n = str.indexOf("welcome");
var m = str.indexOf("itDoesntExist");

console.log(n)  // prints 13
console.log(m) //  prints -1 since it doesnt exist

later you can use an if statement with it. 稍后,您可以使用if语句。

like 喜欢

if(m === -1) { console.log("it doesnt exist ") };    

I guess you meant starting with user- . 我猜你的意思是从user-开始。

txt.split(' ').some((elem) =>  elem.startsWith('user-'));

will give you what you need 会给你你所需要的

Just use indexOf . 只需使用indexOf But why not pass active_user.class directly? 但是,为什么不直接传递active_user.class呢?

 var s1 = 'user-1 color-class'; var s2 = 'text-class user-1 color-class'; var s3 = 'text-class not-user-1 color-class'; var s4 = 'not-user-1 color-class'; var s5 = 'not-user-1 user-1 color-class'; var test1 = 'user-'; var test2 = 'user-1'; // effectively active_user.class var test3 = 'user-2'; function check (pWhat, pAgainst) { var w = ' ' + pWhat; var a = ' ' + pAgainst; return w.indexOf (a) >= 0; } console.log("aginst: " + test1); console.log(s1 + " -> " + check(s1, test1)); console.log(s2 + " -> " + check(s2, test1)); console.log(s3 + " -> " + check(s3, test1)); console.log(s4 + " -> " + check(s4, test1)); console.log(s5 + " -> " + check(s5, test1)); console.log("aginst: " + test2); console.log(s1 + " -> " + check(s1, test2)); console.log(s2 + " -> " + check(s2, test2)); console.log(s3 + " -> " + check(s3, test2)); console.log(s4 + " -> " + check(s4, test2)); console.log(s5 + " -> " + check(s5, test2)); console.log("aginst: " + test3); console.log(s1 + " -> " + check(s1, test3)); console.log(s2 + " -> " + check(s2, test3)); console.log(s3 + " -> " + check(s3, test3)); console.log(s4 + " -> " + check(s4, test3)); console.log(s5 + " -> " + check(s5, test3)); 

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

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