recherche

Maison  >  Questions et réponses  >  le corps du texte

javascript - aide pour les problèmes d'interception et de correspondance réguliers

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

Je souhaite intercepter la chaîne à partir de statique. Comment dois-je écrire l'expression régulière ? Merci

c'est staticca7ecd95-aa95-4da8-b369-92b66b566958icon.png

De plus, étant donné que la première moitié de l'URL peut changer, il est préférable d'en utiliser des normales

習慣沉默習慣沉默2785 Il y a quelques jours985

répondre à tous(6)je répondrai

  • 高洛峰

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

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

    répondre
    0
  • 怪我咯

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

    split('static')[1] Est-ce le cas ? Ou dois-je utiliser des règles régulières ?

    répondre
    0
  • 欧阳克

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

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

    répondre
    0
  • phpcn_u1582

    phpcn_u15822017-07-05 10:55:53

    Vous devez utiliser static.* pour les expressions régulières. Voici le code de référence

    .
    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}`);
        });

    répondre
    0
  • ringa_lee

    ringa_lee2017-07-05 10:55:53

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

    répondre
    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"
    ]

    Points forts de cette question :

    répondre
    0
  • Annulerrépondre