Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
182 views
in Technique[技术] by (71.8m points)

Javascript - how to use regex process the following complicated string

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

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

1 Reply

0 votes
by (71.8m points)

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`));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...