简体   繁体   English

从字符串中删除不是名字/姓氏字符的字符

[英]Remove characters from a string that are not firstname/surname characters

Please see the code below:请看下面的代码:

 @HostListener('paste', ['$event'])
  onPaste(event) {
    var test = event.clipboardData.getData('text');
    var removedNumbers = test.replace(/[0-9]/g, '');
  }

Numbers are removed from the pasted text.从粘贴的文本中删除数字。 It is a surname field, so should also exclude characters like {[}] etc.它是一个姓氏字段,因此还应排除 {[}] 等字符。

How can I remove characters that are not valid for a name?如何删除对名称无效的字符? I have read lots of simlar questions today like this one: how do i block or restrict special characters from input fields with jquery?我今天已经阅读了很多类似的问题,例如: 如何使用 jquery 阻止或限制输入字段中的特殊字符? . . However, I have not found an answer to my specific question.但是,我还没有找到我的具体问题的答案。

[^ ] matches anything(including space) that is not enclosed in the brackets, so you could place all characters you don't want to be removed inside the bracket. [^ ]匹配未括在方括号中的任何内容(包括空格),因此您可以将所有不想删除的字符放在方括号内。 Note, however, that you have to escape special characters if they are part of the match.但是请注意,如果特殊字符是匹配的一部分,则必须对其进行转义。 Also note that还要注意的是

you can specify a range of characters by using a hyphen, but if the hyphen appears as the first or last character enclosed in the square brackets it is taken as a literal hyphen to be included in the character set as a normal character.您可以使用连字符指定一个字符范围,但如果连字符显示为包含在方括号中的第一个或最后一个字符,则它被视为文字连字符,作为普通字符包含在字符集中。

 const regex = /[^az,' -]/gi; console.log("Conan O'Brien".replace(regex, ''));

You may also use Unicode character ranges for non-English names, for example您还可以将 Unicode 字符范围用于非英语名称,例如

  • for Chines 4e00 to 9fa5 ,对于中文4e00 to 9fa5
  • for most of Latin 0061 to 007A & 00DF to 00F6 & 00F8 to 01BF & 01C4 to 024F大多数拉丁语0061 to 007A & 00DF to 00F6 & 00F8 to 01BF & 01C4 to 024F
  • for Geʽez 1200 to 135A Geʽez 1200 to 135A

 const regexLatin = /[^\a-\z\ß-\ö\ø-\ƿ\DŽ-\ɏ ]/gui; const regexChina = /[^\一-\龥 ]/gui; const regexGeez = /[^\ሀ-\፿ ]/gui; console.log("Björk Guðmundsdóttir".replace(regexLatin, '')); console.log("陳港生".replace(regexChina, '')); console.log("ምኒልክ".replace(regexGeez, ''));

However, this is not an exhaustive list, you may refer to the List_of_Unicode_characters to make adjustments for your specific need.然而,这并不是一个详尽的列表,您可以参考List_of_Unicode_characters来根据您的特定需要进行调整。

Trying to match all names from 'all' languages could be very hard.尝试匹配来自“所有”语言的所有名称可能非常困难。 The good news, however, is that Unicode_Property_Escapes are part of the ECMAScript 2020 Specification( currently on draft stage ) which will simplify the process a lot.然而,好消息是Unicode_Property_Escapes是 ECMAScript 2020 规范(目前处于草案阶段)的一部分,这将大大简化流程。 For example to match for Latin characters, you would use: /\\p{Script=Latin}/u , and to match for letters from 'all' languages, you would use: /\\p{Letter}/gu or the short form /\\p{L}/gu例如,要匹配拉丁字符,您可以使用: /\\p{Script=Latin}/u ,要匹配来自“所有”语言的字母,您可以使用: /\\p{Letter}/gu或缩写形式/\\p{L}/gu

Try this.尝试这个。

Vanilla Javascript原生 JavaScript

 document.addEventListener("paste", event => { event.preventDefault(); let clipboardData = event.clipboardData.getData("Text"); clipboardData = clipboardData.replace(/[0-9_!¡?÷?¿/\\\\+=@#$%\\ˆ&*(){}|~<>;:[\\]]/g, ""); let allowedPasteTarget = ['textarea', 'text'] if (allowedPasteTarget.includes(document.activeElement.type)) { let prevText = document.activeElement.value; document.activeElement.value = prevText + clipboardData; } }); //To handle the copy button, [Optional] document .getElementById("copy-text") .addEventListener("click", function(e) { e.preventDefault(); document.getElementById("text-to-copy").select(); var copied; try { copied = document.execCommand("copy"); } catch (ex) { copied = false; } if (copied) { document.getElementById("copied-text").style.display = "block"; } });
 <div> <input type="text" id="text-to-copy" placeholder="Enter text" /> <button id="copy-text">Copy</button> <span id="copied-text" style="display: none;">Copied!</span> </div> <div> <textarea name="paste-area" id="paste-area" cols="30" rows="10" placeholder="Paste it here"></textarea> </div>

Angular

@HostListener('paste', ['$event'])
onPaste(event) {
  var test = event.clipboardData.getData('text');
  var removedNumbers = test.replace(/[0-9_!¡?÷?¿/\\+=@#$%\ˆ&*(){}|~<>;:[\]]/g, '');
  let allowedPasteTarget = ['textarea', 'text']
    if (allowedPasteTaeget.includes(document.activeElement.type)) {
        let prevText = document.activeElement.value;
        document.activeElement.value = prevText + clipboardData;
    }
}

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

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