简体   繁体   English

当我使用“和”或“或”时,为什么我的替换在javascript中正常工作,但在我使用“&”或“/”或“+”时却没有

[英]Why does my replace work properly in javascript when I use “and” or “or” but not when I use “&” or “/” or “+”

I have a first name input in a HTML form and sometimes a user will input two first names and I need to strip out the second name and the word or character used to join them. 我在HTML表单中输入了第一个名称,有时用户会输入两个名字,我需要删除第二个名称以及用于加入它们的单词或字符。 If I use the word "and" or the word "or" my javascript works fine and strips out the second name and the word used to join them. 如果我使用单词“和”或单词“或”,我的javascript工作正常,并删除第二个名称和用于加入它们的单词。 However, if I use an ampersand (&) or a plus sign (+) or a forward slash (/), no matter how I code it, it will only strip out the last character of the second name and not the joining character or any af the second name except the last character. 但是,如果我使用&符号(&)或加号(+)或正斜杠(/),无论我如何编码它,它只会删除第二个名称的最后一个字符而不是连接字符或除了最后一个字符之外的第二个名字。

I have tried using string functions, regular expressions, testing my regular expression at regex.com (they work there and properly select the words and the joining words), a dozen different variations of my regular expression that all work properly at regex, com to select the text I want removed, but none of this works in my actual form. 我已经尝试过使用字符串函数,正则表达式,在regex.com上测试我的正则表达式(他们在那里工作并正确选择单词和加入的单词),正则表达式的十几种不同变体在regex中正常工作,com到选择我想要删除的文本,但这些都不适用于我的实际形式。

For instance if I input: "Bob and Dave" I get "Bob" - Correct "Bob or Dave" I get "Bob" - Correct 例如,如果我输入:“鲍勃和戴夫”我得到“鲍勃” - 正确“鲍勃或戴夫”我得到“鲍勃” - 正确

But if I input: "Bob & Dave" I get "Bob & Dav" - Incorrect "Bob + Dave" I get "Bob + Dav" - Incorrect "Bob / Dave" I get "Bob " - Incorrect but Acceptable "Bob, Dave" I get "Bob, Dav" - Incorrect 但如果我输入:“Bob&Dave”我得到“Bob&Dav” - 不正确的“Bob + Dave”我得到“Bob + Dav” - 不正确的“Bob / Dave”我得到“Bob” - 不正确但可以接受“Bob,戴夫“我得到”鲍勃,戴维“ - 不正确

This is some of the code I have used, several different approaches have worked for the first two, nothing has worked for the last four. 这是我使用过的一些代码,前两种方法有几种不同的方法,后四种方法都没有。

Works for "Bob and Dave": 适用于“Bob and Dave”:

function noExtraNames() {
var pattern=document.forms[0].first_name.value;
var z = pattern.search(/(\s)*\w+/gi)
var x = pattern.search("AND", z+1)
if (z < x) {
    alert('Please only enter ONLY ONE FIRST NAME')
    document.forms[0].first_name.focus();
    return document.forms[0].first_name.value=pattern.replace(/\s(AND)*\w+/gi, "");
}
}

Works for "Bob or Dave": 适用于“鲍勃或戴夫”:

function noExtraNames() {
var pattern=document.forms[0].first_name.value;
var z = pattern.search(/\s(OR)*\w+/gi)
var x = pattern.search("OR", z+1)
if (z < x) {
    alert('Please only enter ONLY ONE FIRST NAME')
    document.forms[0].first_name.focus();
    return document.forms[0].first_name.value=pattern.replace(/\s(or)*\w+/gi, "");
}
}

This also worked for "Bob and Dave" and changing the corresponding word "and" for "or", worked for "Bob or Dave": 这也适用于“鲍勃和戴夫”,并为“或”更改相应的单词“和”,为“鲍勃或戴夫”工作:

function noExtraNames() {
var pattern=document.forms[0].first_name.value;
var z = pattern.search("and")
var x = pattern.search("and")
if (z < x) {
    alert('Please only enter ONLY ONE FIRST NAME')
    document.forms[0].first_name.focus();
    return document.forms[0].first_name.value=pattern.slice(0, z);
}
}

However, this worked for the incorrect but acceptable "Bob, Dave", but did not work for "Bob & Dave" or "Bob / Dave", when the appropriate characters were substituted: 然而,这适用于不正确但可接受的“Bob,Dave”,但是当替换适当的字符时,“Bob&Dave”或“Bob / Dave”不起作用:

function noExtraNames() {
var pattern=document.forms[0].first_name.value;
var z = pattern.search(/(,\s+)/gi);
var x = pattern.search((/(,\s+)/gi), z-1);
if (z < x) {
    alert('Please only enter ONLY ONE FIRST NAME');
    document.forms[0].first_name.focus();
    return document.forms[0].first_name.value=pattern.replace(/(,\s+)/gi);
}
}

And this yielded the same incorrect result with "&" and "/": 这与“&”和“/”产生了相同的错误结果:

function noExtraNames() {
var pattern=document.forms[0].first_name.value;
var z = pattern.search(/(\s&)/gi);
var x = pattern.search(/(\s&\s.[a-z].+)/gi, z-1);
if (x < z) {
    alert('Please only enter ONLY ONE FIRST NAME');
    document.forms[0].first_name.focus();
    return document.forms[0].first_name.value=pattern.replace(/(\s&\s.[a-z].+)/gi, "");
}
}

The regular expressions all worked to select the proper text at regex.com, but never work in my form and I am almost out of hair to pull in frustration. 正则表达式都努力在regex.com上选择正确的文本,但从不以我的形式工作,我几乎没有头发让人感到沮丧。

Why is this happening and more importantly how can I fix it. 为什么会发生这种情况,更重要的是如何解决这个问题。 I have tried several other solutions I found on this site such as: 我尝试过在本网站上找到的其他几种解决方案,例如:

function noExtraNames() {
var pattern=document.forms[0].first_name.value;
var z = pattern.search(/(,\s+)/gi);
var x = pattern.search((/(,\s+)/gi), z-1);
if (x < z) {
    alert('Please only enter ONLY ONE FIRST NAME');
    document.forms[0].first_name.focus();
    var s = document.forms[0].first_name.value;
    var n = s.indexOf('&');
    s = s.substring(0, n != -1 & n : s.length);
    return document.forms[0].first_name.value = s;
}
}

All for no joy. 一切都没有快乐。

The expected output is: Bob The actual output is: Bob & Dav 预期输出为:Bob实际输出为:Bob&Dav

It seems like this would be better handled by splitting on the regex and returning the first item. 似乎通过拆分正则表达式并返回第一个项目可以更好地处理这个问题。 This pattern should take care of the cases mentioned in your post: 这种模式应该照顾你帖子中提到的案例:

var pattern = /(\s*[\+|&|\,|\/]\s*|\s+(and|or)\s+)/i;

then splitting and always returning the first element. 然后拆分并始终返回第一个元素。

 function noExtraNames(name) { var pattern = /(\\s*[\\+|&|\\,|\\/]\\s*|\\s+(and|or)\\s+)/i; var items = name.split(pattern); if (items.length > 1) { console.log('Please only enter ONLY ONE FIRST NAME'); return items[0]; } return name; } let tests = [ 'Bob', 'Billy Bob', 'Bob and Dave', 'Bob + Dave', 'Bob & Dave', 'Bob / Dave', 'Bob, Dave', 'Igor', 'Andy', 'Bob+Dave', ]; for (let test of tests) { console.log(test, '===> ', noExtraNames(test)); } 

const pattern = /(.*(?=(\s+(and|or)\s+)|(\s*[\&\+,\/\\])))|.*/i;
const patternMatch = str.match(pattern);
if (patternMatch[1]) {
  alert('Please only enter ONLY ONE FIRST NAME');
}
const replaceValue = patternMatch[0].trim();
document.forms[0].first_name.value = replaceValue;

I guess that's what you need. 我猜这就是你需要的。

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

相关问题 当我在函数中使用if语句时,为什么我的javascript无法正常工作 - Why does my javascript not work when I use an if statement in my function 使用锚点时,为什么按键处理程序不起作用 - Why does my keypress handler not work when I use anchors 为什么当我尝试使用 React Native 钩子时,它无法按预期正常工作? - Why when I try to use react native hooks it does not work properly as intended to be? 当我使用replace(“ \\\\”,“ x”)时,它不起作用 - when I use replace(“\\”,“x”) ,it dosenot work 为什么删除回车后我的 javascript 不起作用? - Why does my javascript not work when I remove carriage returns? 当我在代码中使用样式化类时,为什么JavaScript代码不起作用? - Why javascript code not work when I use styled class in my code? 在.exec()上使用.index生成Javascript时,它如何工作? - How does .index work when I use it on the .exec() result in Javascript? 为什么我的代码使用underscore.js但不能使用Ramda.js? - Why does my code work with underscore.js but not when I use Ramda.js? 当我使用“==”时,为什么datepicker突出显示不起作用? - Why does datepicker highlight not work when I use “==”? 使用接头时为什么我的功能不能正常工作? - Why won't my function work when I use splice?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM