Home  >  Q&A  >  body text

Regular expression to match content between two strings

Regular matching
The following is the string

I am code
##javascript
#var a = 1;
console.log(a);
#javascript
##…
I am Chinese:
The weather is so good today

I am code
##javascript
#var b = 1;
console.log(b);
#javascript
# #…

Now we need to get all the content between ##javascript# and #javascript##. How should we write it? Include all spaces and newlines.

I tried [\s\S]*, but if there are multiple similar pieces of code (as shown above), it will directly match the last #javascript## and not recognize the ones in between. . I'm so helpless, please help. .

Halo, how can the segment editor use non-markdown mode. . . Turn all my code into styles

PHPzPHPz2734 days ago918

reply all(1)I'll reply

  • 给我你的怀抱

    给我你的怀抱2017-05-19 10:47:11

    Regularly, you can use exec to extract multiple values, but there will be dirty data, so it is actually more convenient to use split

    var string = '##javascript# var a = 1;console.log(a);#javascript##…我是中文:今天天气真好…我是代码 ##javascript# var b = 1;console.log(b);#javascript##…';
    var arr = string.split("#javascript##").map(function(value){
           return value.match(/##javascript#([\s\S]*)/)? value.match(/##javascript#([\s\S]*)/)[1] : value;
        }
    );
    console.log(arr);

    reply
    0
  • Cancelreply