简体   繁体   English

正则表达式JS:两个字符串之间的匹配字符串,包括换行符

[英]Regex JS: Matching string between two strings including newlines

I want to match string between two string with Regex including newlines. 我想用正则表达式匹配两个字符串之间的字符串,包括换行符。

For example, I have the next string: 例如,我有下一个字符串:

{count, plural,
  one {apple}
  other {apples}
} 

I need to get string between plural, and one . 我需要使字符串在plural,one之间。 It will be \\n*space**space* . 这将是\\n*space**space* I tried this Regex: 我尝试过此正则表达式:

/(?:plural,)(.*?)(?:one)/gs

It works, but not in JS. 它有效,但在JS中不起作用。 How to do that with JavaScript? 如何使用JavaScript做到这一点?

To match the everything including newline character, you can use [\\s\\S] or [^] 要匹配包括换行符在内的所有内容,可以使用[\\s\\S][^]

 var str = `{count, plural, one {apple} other {apples} } `; console.log(str.match(/(?:plural,)([\\s\\S]*?)(?:one)/g)); console.log(str.match(/(?:plural,)([^]*?)(?:one)/g)); 

It doesn´t work because your testing with the wrong regex engine. 它不起作用,因为您使用错误的正则表达式引擎进行了测试。

 `/s` does not exist in the JS regex engine, only in pcre

It must be something like: 它一定是这样的:

/(?:plural,)((.|\n)*?)(?:one)/g

Hope it helps. 希望能帮助到你。

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

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