Skip to content
Advertisement

Create patterns to detect the occurrence of sequences so that you can restrict in which cases to replace the detected substrings in the input string

These are the input string examples:

JavaScript

I have tried with a simple replacement, but I think that the cases must be further restricted with a regex pattern so that unwanted replacements are not made…

JavaScript

, and to be able to obtain this string as output…

the correct output for each of these examples:

JavaScript

In this case, the pseudo-pattern is: some number “a m” to replace with the string “am” one or more empty spaces, a period, a comma or directly the end of the string

Cases should also be considered where there may be incompletely written schedules where “am” would be preceded by ":", " :", ": ", " hs", "hs", "hs ", " h.s. ", "h.s.", "h.s. ", " h.s", "h.s" or "h.s " , for example,

JavaScript

Advertisement

Answer

For the first part I made this regex:

JavaScript

It change the “a m” for “am” keeping whatever was before and after.

For the “hs” or “h.s” I did this:

JavaScript

It search for “hs”, “h.s” before “a m”. You can combine both regex, they are pretty similar or use them sequentially.

Let me know if there is any problem.

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement