简体   繁体   English

替换 typescript 中字符串中的所有字符实例?

[英]Replace all instances of character in string in typescript?

I'm trying to replace all full stops in an email with an x character - for example "my.email@email.com" would become "myxemail@emailxcom".我正在尝试用 x 字符替换 email 中的所有句号 - 例如“my.email@email.com”将变为“myxemail@emailxcom”。 Email is set to a string. Email 设置为字符串。
My problem is it's not replacing just full stops, it's replacing every character, so I just get a string of x's.我的问题是它不只是替换句号,而是替换每个字符,所以我只得到一串x。
I can get it working with just one full stop, so I'm assuming I'm wrong on the global instance part.我只需一个句号就可以让它工作,所以我假设我在全局实例部分错了。 Here's my code:这是我的代码:

let re = ".";
let new = email.replace(/re/gi, "x");

I've also tried我也试过

re = /./gi;
new = email.replace(re, "x");

If anyone can shed any light I'd really appreciate it, I've been stuck on this for so long and can't seem to figure out where I'm going wrong.如果有人能阐明我真的很感激,我已经坚持了这么久,似乎无法弄清楚我哪里出错了。

** Edit: Whoops, my new variable was actually called newemail, keyword new wasn't causing the issue! ** 编辑:哎呀,我的新变量实际上被称为 newemail,关键字 new 并没有导致问题!

Your second example is the closest. 你的第二个例子是最接近的。 The first problem is your variable name, new , which happens to be one of JavaScript's reserved keywords (and is instead used to construct objects, like new RegExp or new Set ). 第一个问题是您的变量名称new ,它恰好是JavaScript的保留关键字之一 (而是用于构造对象,如new RegExpnew Set )。 This means that your program will throw a Syntax Error. 这意味着您的程序将抛出语法错误。

Also, since the dot ( . ) is a special character inside regex grammar, you should escape it as \\. 此外,由于点( . )是正则表达式语法中的特殊字符,因此您应该将其转义为\\. . Otherwise you would end up with result == "xxxxxxxxxxxxxxxxxx" , which is undesirable. 否则你最终会得到result == "xxxxxxxxxxxxxxxxxx" ,这是不可取的。

 let email = "my.email@email.com" let re = /\\./gi; let result = email.replace(re, "x"); console.log(result) 

You may just use replaceAll() String function, described here: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll您可以只使用replaceAll() String function,此处描述: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

If you are getting Property 'replaceAll' does not exist on type 'string' error - go to tsconfig.json and within "lib" change or add "es2021".如果您在类型“字符串”错误上出现属性“replaceAll”不存在- go 到tsconfig.json并在“lib”中更改或添加“es2021”。

Like this:像这样:

在此处输入图像描述

More info here: Property 'replaceAll' does not exist on type 'string'此处的更多信息: “字符串”类型上不存在属性“replaceAll”

You can try split() and join() method that was work for me. 您可以尝试对我有用的split()join()方法。 (For normal string text) It was short and simple to implement and understand. (对于普通的字符串文本)实现和理解简短。 Below is an example. 以下是一个例子。

let email = "my.email@email.com";
email.split('.').join('x');

So, it will replace all your . 所以,它将取代你所有的. with x . x So, after the above example, email variable become myxemail@gmailxcom 因此,在上面的例子之后, email变量变为myxemail@gmailxcom

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

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