如何获取仅点的正则表达式?

时间:2018-12-26 12:57:32

标签: javascript regex angular

这里我有一张图像,该图像名称仅是点(...............jpg) 如果imageName只有点,则我得到只有点的imageName(imageNameWithoutExtension请参见cosole),所以我只需要点正则表达式,所以我只能与点名为imageName的点正则表达式进行比较,如何获得点正则表达式?

renameFile.component.ts

updateFileName(){

  console.log(imageName);  // ...............jpg 
  var imageArray = imageName.split('.');
  var imageExtension = imageArray.pop();
  var imageNameWithoutExtension = imageArray.join('.');  
  console.log(imageNameWithoutExtension);  // ...............

   imageNameWithoutExtension.replace(^([,.\d]+)([,.]\d{2})$); i tried this but it gives error, how to use regex or make regex I dont know 

  'image name with only dots' // hot to get regex of only dots

  if(imageNameWithoutExtension == 'image name with only dots'){
    console.log("image name with only dots so not allowed to rename");

  }else{
    console.log("image name with not only dots so allow to rename");
  }
}

2 个答案:

答案 0 :(得分:1)

尝试一下。

imageName.match(/^[\.]+[a-zA-Z0-9]{1,}$/)

如果您的文件可能没有扩展名,那么

imageName.match(/^[\.]+[a-zA-Z0-9]{0,}$/)

答案 1 :(得分:1)

我希望这就是你想要的,

var updateFileName = function () {
    var imageName = "...............jpg"
    console.log(imageName);  // ...............jpg 
    var imageArray = imageName.split('.');
    var imageExtension = imageArray.pop();
    var imageNameWithoutExtension = imageArray.join('.');
    console.log(imageNameWithoutExtension);  // ...............
    var flag = false
    if (imageNameWithoutExtension.match(/^[\.]+$/)) {
        flag = true
    }
    if (flag) {
        console.log("image name with only dots so not allowed to rename");

    } else {
        console.log("image name with not only dots so allow to rename");
    }
}
updateFileName()