Heim  >  Fragen und Antworten  >  Hauptteil

Javascript – Hilfe bei regelmäßigen Abhör- und Matching-Problemen

src\main\webapp\static\ca7ecd95-aa95-4da8-b369-92b66b566958\icon.png

Ich möchte die Zeichenfolge beginnend mit „statisch“ abfangen. Wie soll ich den regulären Ausdruck schreiben? Danke

das ist staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png

Da sich außerdem die erste Hälfte der URL ändern kann, ist es am besten, normale URLs zu verwenden

習慣沉默習慣沉默2662 Tage vor902

Antworte allen(6)Ich werde antworten

  • 高洛峰

    高洛峰2017-07-05 10:55:53

    var str='src\\main\\webapp\\static\\ca7ecd95-aa95-4da8-b369-92b66b566958\\icon.png';
    alert(str.replace(/^.*?(static.*?)$/ig, '$1'));

    Antwort
    0
  • 怪我咯

    怪我咯2017-07-05 10:55:53

    split('static')[1] 这样的吗? 还是必须用正则?

    Antwort
    0
  • 欧阳克

    欧阳克2017-07-05 10:55:53

    str.slice(str.search(/static/));

    Antwort
    0
  • phpcn_u1582

    phpcn_u15822017-07-05 10:55:53

    正则应该用 static.* 就可以,下面是参考代码

    const regex = /static.*/g;
    const str = `src\\main\\webapp\\static\\ca7ecd95-aa95-4da8-b369-92b66b566958\\icon.png`;
    let m;
    
    while ((m = regex.exec(str)) !== null) {
        // This is necessary to avoid infinite loops with zero-width matches
        if (m.index === regex.lastIndex) {
            regex.lastIndex++;
        }
        
        // The result can be accessed through the `m`-variable.
        m.forEach((match, groupIndex) => {
            console.log(`Found match, group ${groupIndex}: ${match}`);
        });

    Antwort
    0
  • ringa_lee

    ringa_lee2017-07-05 10:55:53

    'srcmainwebappstaticca7ecd95-aa95-4da8-b369-92b66b566958icon.png'.split('static')[1]

    Antwort
    0
  • 曾经蜡笔没有小新

    曾经蜡笔没有小新2017-07-05 10:55:53

    'src\\main\\webapp\\static\\ca7ecd95-aa95-4da8-b369-92b66b566958\\icon.png'.match(/static.*/)
    
    // Output: 
    [
      "static\\ca7ecd95-aa95-4da8-b369-92b66b566958\\icon.png"
    ]

    这个问题的亮点:

    Antwort
    0
  • StornierenAntwort