开发者

looking for a regular expression to extract all text outputs to user from js file

开发者 https://www.devze.com 2023-01-31 05:54 出处:网络
i have some huge js files and there are some texts/messages/... which are output for a human beeing. the problem is they don\'t run over the same method.

i have some huge js files and there are some texts/messages/... which are output for a human beeing. the problem is they don't run over the same method.

but i want to find them all to refactor the code. now i am searching for a regular expression to find those messages.

...his.submit_register = function(){
        if(!this.agb_accept.che开发者_如何学Ccked) {
            out_message("This is a Messge tot the User in English." , "And the Title of the Box. In English as well");
            return fals;
        }
        this.valida...

what i want to find is all the strings which are not source code. in this case i want as return:

  • This is a Messge tot the User in

  • English. And the Title of the Box. In English as well

i tried something like: /\"(\S+\s{1})+\S\"/, but this wont work ...

thanks for help


It's not possible to parse Javascript source code using regular expressions because Javascript is not a regular language. You can write a regular expression that works most of the time:

/"(.*?)"/

The ? means that the match is not greedy.

Note: this will not correctly handle strings that contain ecaped quotes.


A simple java regex solving your problem (assuming that the message doesn't contain a " character):

Pattern p = Pattern.compile("\"(.+?)\"");

The extraction code :

Matcher m;

for(String line : lines) {
    m = p.matcher(line);
    while(m.find()) {
        System.out.println(m.group(1));
    }
}
0

精彩评论

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