简体   繁体   English

在Regex中如何在不在其他字母字符内时查找和替换字符?

[英]In Regex How to find and replace characters when they are not within other alphabetic characters?

I'm formatting a datetime string in javascript by Regex , so I want: 我正在用Regexjavascript格式化日期时间字符串,所以我想要:

  1. Find and replace all d characters when d is not within other alphabetic characters. d不在其他字母字符内时,查找并替换所有d字符。 like this: 像这样: 在此处输入图片说明

  2. Find and replace all dd characters when dd is not within other alphabetic characters. dd不在其他字母字符内时,查找并替换所有dd字符。 like this: 像这样: 在此处输入图片说明


I tested /\\bd\\b/mg pattern but its result is not which I want everytime. 我测试了/\\bd\\b/mg模式,但是它的结果并不是我每次都想要的。

How should I exclude unwanted cases in the following command: 如何在以下命令中排除不需要的情况:

str = str.replace(/\bd\b/mg, number);

The regular expression You posted does not consider _ as a word boundary, so it does not replace the character as expected. 您发布的正则表达式不会将_视为单词边界,因此不会按预期替换该字符。

In order to include this character as well, either before or after the d character to be replaced, You can use expressions similar to these: 为了在要替换的d字符之前或之后也包含此字符,可以使用类似于以下的表达式:

  1. To replace d : 替换d

     /(\\b|_)(d)(\\b|_)/mg 
  2. To replace dd : 替换dd

     /(\\b|_)(dd)(\\b|_)/mg 

Or to replace both in the same way (if it's acceptable): 或以相同的方式替换两者(如果可以):

    /(\b|_)(d|dd)(\b|_)/mg

In comments under this answer in another thread on StackOverflow , it was also suggested to use a library that can format dates, instead of implementing it by Yourself. StackOverflow上另一个线程中此答案下的注释 ,还建议使用可以格式化日期的库,而不是自己实现。

UPDATE: As someone mentioned, the issue with this is also that including _ in the regular expression, removes it after the replacement. 更新:正如有人提到的那样,与此有关的问题还在于,在正则表达式中包含_ ,在替换后将其删除。 However, You can call replace and use capturing parentheses references, like this: 但是,您可以调用replace并使用捕获括号引用,如下所示:

    str.replace(/(\b|_)(d)(\b|_)/mg, "$1" + number + "$3")

I've updated earlier expressions posted in this answer to work with this method. 我已经更新了此答案中发布的早期表达式,以使用此方法。

Please note that I'm not aware of all the cases You want to consider, so if You have any problems using the suggested solution or it does not work as expected in Your case, please let me know, so I can try to help. 请注意,我不知道您想考虑的所有情况,因此,如果您在使用建议的解决方案时遇到任何问题,或者在您的情况下无法正常工作,请告诉我,以便我们为您提供帮助。

I could use a lookahead and if you are not using JavaScript then a lookbehind as well. 我可以先行使用,如果您不使用JavaScript,那么也可以先行使用。

example lookahead which checks if there is no following alpha character: 前瞻示例,检查是否没有以下字母字符:

(?=[^a-zA-Z])

If you are using JavaScript it doesn't support lookbehind so you will need to use a capturing group and backreferencing. 如果您使用的是JavaScript,则它不支持向后搜索,因此您将需要使用捕获组和反向引用。

For JS capture the part in the outermost parentheses and then use \\1, \\2... to target: 对于JS,请在最外面的括号中捕获该部分,然后使用\\ 1,\\ 2 ...进行定位:

[^a-zA-Z](d(?=[^a-zA-Z]))

non-JS can use lookbehind: 非JS可以使用lookbehind:

(?<=[^a-zA-Z])d(?=[^a-zA-Z])

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

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