开发者

getting contents of string between digits

开发者 https://www.devze.com 2023-04-02 09:50 出处:网络
have a regex problem :( what i would like to do is to find out the contents between two or more numbers.

have a regex problem :(

what i would like to do is to find out the contents between two or more numbers.

var string = "90+*-+80-+/*70"

im trying to edit the symbols in between so it only shows up the last symbol and not the ones before it. so trying to get the above variable to be turned into 90+80*70. although this is just an example i have no idea how to do this. the length of the 开发者_Go百科numbers, how many "sets" of numbers and the length of the symbols in between could be anything.

many thanks,

Steve,


The trick is in matching '90+-+' and '80-+/' seperately, and selecting only the number and the last constant.

The expression for finding the a number followed by 1 or more non-numbers would be

\d+[^\d]+

To select the number and the last non-number, add parens:

(\d+)[^\d]*([^\d])

Finally add a /g to repeat the procedure for each match, and replace it with the 2 matched groups for each match:


js> '90+*-+80-+/*70'.replace(/(\d+)[^\d]*([^\d])/g, '$1$2');   
90+80*70
js>


Or you can use lookahead assertion and simply remove all non-numerical characters which are not last: "90+*-+80-+/*70".replace(/[^0-9]+(?=[^0-9])/g,'');


You can use a regular expression to match the non-digits and a callback function to process the match and decide what to replace:

var test = "90+*-+80-+/*70";
var out = test.replace(/[^\d]+/g, function(str) {
    return(str.substr(-1));
})

alert(out);

See it work here: http://jsfiddle.net/jfriend00/Tncya/

This works by using a regular expression to match sequences of non-digits and then replacing that sequence of non-digits with the last character in the matched sequence.


i would use this tutorial, first, then review this for javascript-specific regex questions.


This should do it -

var string = "90+*-+80-+/*70"
var result = '';
var arr = string.split(/(\d+)/)
for (i = 0; i < arr.length; i++) {
    if (!isNaN(arr[i])) result = result + arr[i];
    else result = result + arr[i].slice(arr[i].length - 1, arr[i].length);
}
alert(result);

Working demo - http://jsfiddle.net/ipr101/SA2pR/


  1. Similar to @Arnout Engelen

    var string = "90+*-+80-+/*70";
    
    string = string.replace(/(\d+)[^\d]*([^\d])(?=\d+)/g, '$1$2'); 
    

    This was my first thinking of how the RegEx should perform, it also looks ahead to make sure the non-digit pattern is followed by another digit, which is what the question asked for (between two numbers)

  2. Similar to @jfriend00

    var string = "90+*-+80-+/*70";
    
    string = string.replace( /(\d+?)([^\d]+?)(?=\d+)/g
                           , function(){
                                return arguments[1] + arguments[2].substr(-1);
                           });
    

    Instead of only matching on non-digits, it matches on non-digits between two numbers, which is what the question asked


Why would this be any better?

  1. If your equation was embedded in a paragraph or string of text. Like:

    This is a test where I want to clean up something like 90+*-+80-+/*70 and don't want to scrap the whole paragraph.

    Result (Expected) :

    This is a test where I want to clean up something like 90+80*70 and don't want to scrap the whole paragraph.

Why would this not be any better?

  1. There is more pattern matching, which makes it theoretically slower (negligible)
  2. It would fail if your paragraph had embedded numbers. Like:

    This is a paragraph where Sally bought 4 eggs from the supermarket, but only 3 of them made it back in one piece.

    Result (Unexpected):

    This is a paragraph where Sally bought 4 3 of them made it back in one piece.

0

精彩评论

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