开发者

Regex pattern matching (Javascript/ASP)

开发者 https://www.devze.com 2022-12-19 13:17 出处:网络
I need some help with a regular expression, please help if you can I have the following code: I am using Javascript and ASP

I need some help with a regular expression, please help if you can

I have the following code: I am using Javascript and ASP

{In|inside|during|into|in the sphere of} {this} {article|piece of writing|editorial|commentary|paragraph|section} {we} {will|desire to|wishto|want to|resolve to|will} {tell} {you} {more} {about|regarding|with reference to} {the}

The desired code should look like this:

{In|inside|during|into|in the sphere of} this {article|piece of writing|editorial|commentary|paragraph|section} we {will|desire to|wishto|want to|resolve to|will} tell you more {about|regarding|with reference to} the

The brackets around the single words with no | should be removed like - this - we - tell you more - the in the example above.

I am thinki开发者_高级运维ng that the solution should be something like this

replace(/{.+?[^\|]/ig, '');   

to replace the { there should not be a | in the code; {.+?[^\|] and replace { with nothing

Then if there is not a starting { to replace the } with nothing

I am not sure how to do this, and how to only remove the {} where there is no | inside without removing the content...


x.replace(/{([^|}]*)}/g, '$1')


Try:

var string = "{hello|there} {yes} {no|me} {ok}";
string = string.replace(/{[A-Za-z0-9]+)}/g, "$1");

Gives you:

{hello|there} yes {no|me} ok
0

精彩评论

暂无评论...
验证码 换一张
取 消