Basic regular expression in JavaScript

后端 未结 6 1988
[愿得一人]
[愿得一人] 2021-01-23 04:55

For some time I\'ve been trying to understand regular expressions in JavaScript, but it is extremely complicated. You could tell me how do I redeem separately each of the values

相关标签:
6条回答
  • 2021-01-23 05:36

    just use split()

    var mystring = "/first/middle/last"
    
    var mysplitstring = mystring.split("/");
    
    var first = mysplitstring[1];
    var middle = mysplitstring[2];
    var last = mysplitstring[3];
    
    0 讨论(0)
  • This won't answer your question, but you can use this awesome tool to generate and test regular expressions. It always come in handy when i need to use a regEx.

    0 讨论(0)
  • 2021-01-23 05:39

    This is certainly easier to do using strings' split method, but here's one way of doing it with a regexp for learning purposes:

    var url = '/first/middle/last';
    var regex = /^\/(.+)\/(.+)\/(.+)$/;
    regex.exec(url); // ["/first/middle/last", "first", "middle", "last"]
    

    And to help you understand this regexp (and hopefully regexps in general), let's look at it in closer detail.

    The first and last slashes (/) mark the beginning and end of the RegExp object (excluding the modifiers, which you can probably do without for now), just like " and ' mark the beginning and end of a string.

    The ^ character at the beginning means "beginning of string", so it means a match for this regexp will have to be in the beginning of the string.

    The $ character in the end means "end of string", so the match has to end to the end of the string. This combined with the ^ character in the beginning means the match will have to cover the entire string.

    The rest of the regexp is the \/(.+) part three times. The first two characters (\/) simply match a slash. We cannot match a slash with / directly, because that would end the regular expression, so we escape it using a backslash. The .+ part matches any character except a newline character (.) one or more times (+). The parentheses around that part are called capturing parentheses and make the string they captured be remembered, in other words they make "first", "middle", and "last" appear in the resulting array.

    So the entire regexp matches "beginning of string, slash, one or more characters (remember these), slash, one or more characters (remember these), slash, one or more characters (remember these), end of string".

    You can find more information about the special characters (dot, plus, parentheses, backslash, etc.) in regular expressions from for example MDN's RegExp documentation.

    0 讨论(0)
  • 2021-01-23 05:44

    I think you should use split() for this kind of thing, but if you really like to use regexp you could do this:

     var url = "/first/middle/last";
     var reg = /\/(.*)\/(.*)\/(.*)/;
     var matches = reg.exec(url); 
     // matches[0] == first
     // matches[1] == middle
     // matches[2] == last
    
    0 讨论(0)
  • 2021-01-23 05:47

    Here you go, try this out:

    var string = "/first/middle/last";
    var pattern = /([^\/]+)/g;
    var result = string.match(pattern);
    
    for (var i = 0; i < result.length; ++i) {
        var value = result[i];
        alert(value);
    } 
    

    Also you can check out this demo

    0 讨论(0)
  • 2021-01-23 05:50

    I don't know if this answers your question, but what you're trying to achieve is best done with split:

    var parts = "/first/middle/last".split("/");
    
    var first = split[1];
    var middle = split[2];
    var last = split[3];
    

    Note that index would start at 0, but that will contain anything before the first occurrence of the split string. In your example, that would just be an empty string, since the string starts with a slash.

    0 讨论(0)
提交回复
热议问题