简体   繁体   English

javascript正则表达式以检查字符和数字以外的其他字符

[英]javascript regular expression to check other than characters and numbers

I need to compare one string with an other string. 我需要将一个字符串与另一字符串进行比较。

Suppose textbox1 contains "this is my requirement" and textbox2 contains "check textbox ". 假设textbox1包含"this is my requirement"textbox2包含"check textbox ”。

I need to take first word of each textbox (ie this and check ) and compare them. 我需要每一个文本框的第一个字(即thischeck ),并进行比较。 An exception should be thrown if they are not the same. 如果它们不同,则应引发异常。

The texts might consist of special characters, numbers, anything, but I only need the first words of these texts. 这些文本可能包含特殊字符,数字或其他任何内容,但我只需要这些文本的开头几个字即可。

Could anybody tell me a regular expression for this? 有人可以告诉我一个正则表达式吗?

/^(\S+)/.exec(str)[1]

that will give you the first word of any string, and then you can compare the two... is that what you want? 这将为您提供任何字符串的第一个单词,然后您可以将两者进行比较...这就是您想要的吗? Or if you want to ignore leading whitespace: 或者,如果您想忽略前导空格:

/^\s*(\S+)/

eg: 例如:

var rx = /^(\S+)/;

if(rx.exec(textbox1.value)[1] != rx.exec(textbox2.value)[1]){
    // do stuff
}

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

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