正则表达式:如何匹配整个单词只有一个破折号不超过1破折号?

时间:2018-06-01 14:50:25

标签: regex

我有这些话

verification-asdflkaasidmf
verification-sadfim-aisdmfim

我想匹配第一个verification-asdflkaasidmf但不匹配第二个(有两个破折号)

2 个答案:

答案 0 :(得分:2)

另一种方法是:

import { AngularFireAuth } from 'angularfire2/auth';
model : any = {} ;
private afAuth : AngularFireAuth,

regWithEP () {
    this.afAuth.auth.createUserWithEmailAndPassword(this.model.email, this.model.password).then((user) => {
    /* IMPORTANT !! */
    /* EXPLICIT CHECK IF USER IS RETURNED FROM FIREBASE SERVICE !! */

          if (user) {
            console.log(user);
            /* Here user is available and is the same as auth.currentUser */

            this.afAuth.auth.currentUser.getToken().then((token) => {

            //token is available here
            //set token to currentUser, signIn , re-direct etc.
            });
        }
    });
}

其中包括以下内容:

^[^-]*\-[^-]*$

答案 1 :(得分:2)

您可以使用此正则表达式:

/^\w+-\w+$/

此正则表达式匹配开头的单词,后跟连字符,后跟另一个单词。

let arr = ['verification-asdflkaasidmf',
'verification-sadfim-aisdmfim'
];

let regex = /^\w+-\w+$/;

for (i=0; i<arr.length; i++) {
  console.log(arr[i], regex.test(arr[i]));
}