检查字符串是否等于单词和数字

时间:2021-11-22 21:44:23

I have an object that contains a string as a property. I want to check that this property is not equal to some word, followed by a space and a number. For instance, something like this:

我有一个包含字符串作为属性的对象。我想检查这个属性是不是等于某个单词,后跟一个空格和一个数字。例如,像这样:

var TheWordToCheck = "SomeWord";

if (TheObject['SomeProperty'] !== (TheWordToCheck + ' ' + 2)) {...}
if (TheObject['SomeProperty'] !== (TheWordToCheck + ' ' + 3)) {...}

In this example, the code checks for only "SomeWord 2" and "SomeWord 3". How can I simplify this where it checks any numbers?

在此示例中,代码仅检查“SomeWord 2”和“SomeWord 3”。如何在检查任何数字时简化此操作?

Thanks.

谢谢。

3 个解决方案

#1


2  

You could use a regex and the match() method (untested)

你可以使用正则表达式和match()方法(未经测试)

var reg = new RegExp("^"+TheWordToCheck+"\\s\\d$")

if (!TheObject['SomeProperty'].match(reg) {...

FIDDLE

小提琴

#2


0  

depends on the range of numbers you need to check, if it is static or is less than a maximum value, you can use a loop and append the loop variable with the string and check

取决于您需要检查的数字范围,如果它是静态的或小于最大值,您可以使用循环并将循环变量附加到字符串并检查

for (var i=0;i<maxNumber;i++)
{ 
if (TheObject['SomeProperty'] !== (TheWordToCheck + ' ' + i)) {...
                 break;
                 }
}

or you can use regex as suggested in the comments

或者您可以按照评论中的建议使用正则表达式

#3


0  

You can use a regular expression to check this:

您可以使用正则表达式来检查:

var TheWordToCheck = "SomeWord";
var TheObject = {
    "SomeProperty": "SomeWord 100"
};

var pattern = new RegExp(TheWordToCheck + ' \\d', 'g');
if (TheObject['SomeProperty'].match(pattern) != null) { ... }

Note that you have to do the backslashes twice in order to make sure that the first one is escaped in the pattern. You should also use the RegEx constructor in order to be able to use a variable in your pattern.

请注意,您必须执行两次反斜杠,以确保第一个在模式中转义。您还应该使用RegEx构造函数,以便能够在模式中使用变量。

#1


2  

You could use a regex and the match() method (untested)

你可以使用正则表达式和match()方法(未经测试)

var reg = new RegExp("^"+TheWordToCheck+"\\s\\d$")

if (!TheObject['SomeProperty'].match(reg) {...

FIDDLE

小提琴

#2


0  

depends on the range of numbers you need to check, if it is static or is less than a maximum value, you can use a loop and append the loop variable with the string and check

取决于您需要检查的数字范围,如果它是静态的或小于最大值,您可以使用循环并将循环变量附加到字符串并检查

for (var i=0;i<maxNumber;i++)
{ 
if (TheObject['SomeProperty'] !== (TheWordToCheck + ' ' + i)) {...
                 break;
                 }
}

or you can use regex as suggested in the comments

或者您可以按照评论中的建议使用正则表达式

#3


0  

You can use a regular expression to check this:

您可以使用正则表达式来检查:

var TheWordToCheck = "SomeWord";
var TheObject = {
    "SomeProperty": "SomeWord 100"
};

var pattern = new RegExp(TheWordToCheck + ' \\d', 'g');
if (TheObject['SomeProperty'].match(pattern) != null) { ... }

Note that you have to do the backslashes twice in order to make sure that the first one is escaped in the pattern. You should also use the RegEx constructor in order to be able to use a variable in your pattern.

请注意,您必须执行两次反斜杠,以确保第一个在模式中转义。您还应该使用RegEx构造函数,以便能够在模式中使用变量。