如何在 JavaScript 中消除所有分配的 charector,例如字符串中的 " " ,,{}



我有一个句子,我们如何格式化字符串来支持和分配给一个变量?

let words="2021-08-03 14:00:49.0430 Info Message Template Auto Format enabled
2021-08-03 14:00:49.4297 Info Adding target ConsoleTarget(Name=consoleLogger)
2021-08-03 14:00:49.8396 Info Found 48 configuration items
2021-08-03 14:00:49.8716 Info Configuration initialized.
2021-08-03 14:00:49.8716 Info NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c. File version: 4.6.8.10751. Product version: 4.6.8.
{ "time": "2021-08-03 14:00:50.0625", "thread": "1", "level": "WARN", "message": "Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.", "properties": "path=/root/.aspnet/DataProtection-Keys, EventId_Id=60, EventId=60" }
{ "time": "2021-08-03 14:00:50.4593", "thread": "1", "level": "WARN", "message": "No XML encryptor configured. Key {153434f7-eab5-4326-a0aa-5973fb78b`ecb} may be persisted to storage in unencrypted form.", "proper`ties": "KeyId=153434f7-eab5-4326-a0aa-5973fb78becb, EventId_Id=35, EventId=35" }
{ "time": "2021-08-03 14:01:14.0448", "thread": "5", "level": "WARN", "message": "Failed to determine the https port for redirect.", "properties": "EventId_Id=3, EventId_Name=FailedToDeterminePort, EventId=FailedToDeterminePort" }"
console.log(words)

当我试图做一些字符串方法在它不允许我执行任何操作,因为在句子中的特殊字符。

如何支持这种类型的句子执行字符串操作?

期望输出:I should be able the log.

您可能需要在开始处使用backtick(`)和结束并在文本中间的任何backtick之前添加转义序列。下面是代码片段:

let words = `"2021-08-03 14:00:49.0430 Info Message Template Auto Format enabled
2021 - 08 - 03 14: 00: 49.4297 Info Adding target ConsoleTarget(Name = consoleLogger)
2021 - 08 - 03 14: 00: 49.8396 Info Found 48 configuration items
2021 - 08 - 03 14: 00: 49.8716 Info Configuration initialized.
2021 - 08 - 03 14: 00: 49.8716 Info NLog, Version = 4.0.0.0, Culture = neutral, PublicKeyToken = 5120e14c03d0593c.File version: 4.6.8.10751.Product version: 4.6.8.
{ "time": "2021-08-03 14:00:50.0625", "thread": "1", "level": "WARN", "message": "Storing keys in a directory '/root/.aspnet/DataProtection-Keys' that may not be persisted outside of the container. Protected data will be unavailable when container is destroyed.", "properties": "path=/root/.aspnet/DataProtection-Keys, EventId_Id=60, EventId=60" }
{ "time": "2021-08-03 14:00:50.4593", "thread": "1", "level": "WARN", "message": "No XML encryptor configured. Key {153434f7-eab5-4326-a0aa-5973fb78b`ecb} may be persisted to storage in unencrypted form.", "proper`ties": "KeyId=153434f7-eab5-4326-a0aa-5973fb78becb, EventId_Id=35, EventId=35" }
{ "time": "2021-08-03 14:01:14.0448", "thread": "5", "level": "WARN", "message": "Failed to determine the https port for redirect.", "properties": "EventId_Id=3, EventId_Name=FailedToDeterminePort, EventId=FailedToDeterminePort" } "
`
console.log(words)

最新更新