简体   繁体   English

Javascript:删除字符串之间的空格,除了一个

[英]Javascript: Remove whitespace between strings except one

There are two fields on one page (first name and last name) and one field on another page (org legal name). 一页上有两个字段(名字和姓氏),另一页上有一个字段(组织机构法定名称)。 When the user types in the org legal name field I am checking whether the first name, last name, or a concatenation of the two matched what they are typing for the org legal name . 当用户在org法定名称字段中键入时,我正在检查名字,姓氏或两者的串联是否与他们为org合法名称键入的内容匹配。

My concern is about the concatenation. 我担心的是串联。 Right now it is only testing the concatenation if there is one space between the names (ie. John Smith). 现在,它仅测试名称之间是否存在一个空格(例如John Smith)。 But what if the user types multiple spaces in between by mistake? 但是,如果用户错误地在两者之间键入多个空格怎么办?

I wrote the below method that attempts to match a first name, last name, or a concatenation of first and last names. 我写了下面的方法,尝试匹配名字,姓氏或名字和姓氏的串联。 It works but the problem is that it will only match the concatenation if there is one space between the strings. 它可以工作,但问题是,如果字符串之间只有一个空格,它将仅与串联匹配。 I need it to match in case the user mistakenly types multiple spaces between their first and last name? 如果用户在名字和姓氏之间错误键入多个空格,我需要匹配它吗?

validateFirstAndLastNameMatch() {
    const prospect = this.prospectService.getProspect();
    const firstName = prospect.prospectQuoteDto.firstName.toUpperCase();
    const lastName = prospect.prospectQuoteDto.lastName.toUpperCase();
    const orgLegalName = this.form.get('orgLegalName').value.trim().toUpperCase();
    if (firstName === orgLegalName || lastName === orgLegalName || firstName + " " + lastName === orgLegalName) {
      this.isMatchOnName = true;
    } else {
      this.isMatchOnName = false;
    }    
  }

You could use a regular expression: 您可以使用正则表达式:

 const nameRegexp = /(\\w+)(?:\\s+(\\w+))?/; console.log( 'joe'.match(nameRegexp), 'joe schmoe'.match(nameRegexp), ' joe schmoe '.match(nameRegexp) ); 

Here's a breakdown of the regexp: 这是regexp的分解:

(\\w+) - Match one or more letters for the first name. (\\w+) -将一个或多个字母匹配为名字。

(?:\\s+(\\w+))? - Optionally look for one or more spaces followed by one or more letters and match just the letters for the last name. -(可选)查找一个或多个空格,后跟一个或多个字母,并仅匹配姓氏的字母。

If I'm understanding your question properly, you can just trim the firstName and lastName before comparing them to orgLegalName 如果我正确理解了您的问题,则可以在将firstName和lastName与orgLegalName进行比较之前对其进行修剪

validateFirstAndLastNameMatch() {
const prospect = this.prospectService.getProspect();
const firstName = prospect.prospectQuoteDto.firstName.toUpperCase().trim();
const lastName = prospect.prospectQuoteDto.lastName.toUpperCase().trim();
const orgLegalName = this.form.get('orgLegalName').value.trim().toUpperCase();
if (firstName  === orgLegalName || lastName === orgLegalName || firstName + " " + lastName === orgLegalName) {
  this.isMatchOnName = true;
} else {
  this.isMatchOnName = false;
}    

} }

Match first name and last name combination, allow multiple spaces in between, ignore case. 匹配名字和姓氏的组合,中间允许有多个空格,忽略大小写。

const pattern = new RegExp(firstName +"[ ]+" + lastName, "i");
const isLegal = pattern.test(orgLegalName);

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

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