search

Home  >  Q&A  >  body text

javascript - Can't match the regular expression of js forward pre-check?

Purpose: Remove the spaces to the right of the curly braces.
The following can be used:
//The first
var a = "I am a space {I am a parameter} There are spaces in front of me {I am a parameter} and the spaces in front of me do not match";
hand_trim1(a);
function hand_trim1(str){

return str.replace(/(?:\})\s+/g,'}');

}

//Second
var a = "I am a space {I am a parameter}. There are spaces in front of me {I am a parameter}. The spaces in front of me do not match";
hand_trim2(a);
function hand_trim2(str){

return str.replace(/}\s+/g,'}');

}

//The third one (Why doesn’t this match???????)
var a = "I am a space {I am a parameter} I am in front of {I It is a parameter} There are spaces. The spaces in front of me do not match ";
hand_trim3(a);
function hand_trim3(str){

return str.replace(/(?=\})\s+/g,'');

}

代言代言2713 days ago805

reply all(1)I'll reply

  • 仅有的幸福

    仅有的幸福2017-06-26 10:58:06

    Forward preview is zero width, which means the width is 0, but matches a position, as long as the position is satisfied, and does not match specific content;
    (?=})The matching position should be There is a } after this position;
    s+ means that starting from this position, it will be followed by a blank character;
    So, the position that satisfies the above two conditions is that the character after this position is }, and it is Blank character, therefore, this position does not exist at all, so it does not match;
    In fact, this position should be a } on the left, and s+ on the right, but js does not have a negative assertion, and the left side cannot be predicted character type.

    reply
    0
  • Cancelreply