search

Home  >  Q&A  >  body text

About merging two regular expressions in javascript

When using karma for code coverage testing, remove main.js

// require all src files except main.js for coverage.
// you can also change this to match only the subset of files that
// you want coverage for.
const srcContext = require.context('../../src', true, /^\.\/(?!main(\.js)?$)/)
srcContext.keys().forEach(srcContext)

Later I added a style folder, which contains scss files, and I also want to remove this folder too

ss = ["./main.js","./style","./maaa.js","./style/sss.scss"]

ss.forEach(function(s){
console.log(s)
console.log(/^\.\/(?!main(\.js)?$)/.test(s))
console.log(/^\.\/(?!style.*)/.test(s))
console.log(/^\.\/(?!main(\.js)?$) | ^\.\/(?!style.*)/.test(s))
})
./main.js
false
true
false
./style
true
false
false
./maaa.js
true
true
false
./style/sss.scss
true
false
false

The two separate regular expressions seem to be correct, but using | together does not achieve the expected effect. There is really no other way. Please help me.

世界只因有你世界只因有你2732 days ago761

reply all(2)I'll reply

  • 代言

    代言2017-06-12 09:30:45

    Remove the extra spaces in the middle and enclose | in parentheses on both sides.

    reply
    0
  • 我想大声告诉你

    我想大声告诉你2017-06-12 09:30:45

    Just enclose the body of the regular expression with "[]"
    For example:

    console.log(/^[\.\/(?!main(\.js)?$)|^\.\/(?!style.*)]/.test(s))

    reply
    0
  • Cancelreply