Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have the following string that will occur repeatedly in a larger string:

[SM_g]word[SM_h].[SM_l] "

Notice in this string after the phrase "[SM_g]word[Sm_h]" there are three components:

  1. A period (.) This could also be a comma (,)
  2. [SM_l]
  3. "

Zero to all three of these components will always appear after "[SM_g]word[SM_h]". However, they can also appear in any order after "[SM_g]word[SM_h]". For example, the string could also be:

[SM_g]word[SM_h][SM_l]"

or

[SM_g]word[SM_h]"[SM_l].

or

[SM_g]word[SM_h]".

or

[SM_g]word[SM_h][SM_1].

or

[SM_g]word[SM_h].

or simply just

[SM_g]word[SM_h]

These are just some of the examples. The point is that there are three different components (more if you consider the period can also be a comma) that can appear after "[SM_h]word[SM_g]" where these three components can be in any order and sometimes one, two, or all three of the components will be missing.

Not only that, sometimes there will be up to one space before " and the previous component/[SM_g]word[SM_h].

For example:

[SM_g]word[SM_h] ".

or

[SM_g]word[SM_h][SM_l] ".

etc. etc.

I am trying to process this string by moving each of the three components inside of the core string (and preserving the space, in case there is a space before " and the previous component/[SM_g]word[SM_h]).

For example, [SM_g]word[SM_h].[SM_l]" would turn into

[SM_g]word.[SM_l]"[SM_h]

or

[SM_g]word[SM_h]"[SM_l]. would turn into

[SM_g]word"[SM_l].[SM_h]

or, to simulate having a space before "

[SM_g]word[SM_h] ".

would turn into

[SM_g]word ".[SM_h]

and so on.

I've tried several combinations of regex expressions, and none of them have worked.

Does anyone have advice?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
169 views
Welcome To Ask or Share your Answers For Others

1 Answer

You need to put each component within an alternation in a grouping construct with maximum match try of 3 if it is necessary:

[SM_g]word([SM_h])((?:.|[SM_l]| ?"){0,3})

You may replace word with .*? if it is not a constant or specific keyword.

Then in replacement string you should do:

$1$3$2

var re = /([SM_g]word)([SM_h])((?:.|[SM_l]| ?"){0,3})/g;
var str = `[SM_g]word[SM_h][SM_l] ".`;

console.log(str.replace(re, `$1$3$2`));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...