Matches a string with one . :
/^[^.]*\.[^.]*$/
Same thing for _ :
/^[^_]*_[^_]*/
The combined regex should look something like this:
/^([^._]*\.[^._]*_[^._]*)|([^._]*_[^._]*\.[^._]*)$/
It should now be obvious that indexOf is a better solution, being simpler (performance does not matter until it is shown that this is a bottleneck).
If interested, notice how the combined regular expression has two members, for "string with one . Before one _ " and vice versa. He would have six for three characters, and n! for n. It would be easier to run both regular expressions and the result than using a combined regular expression.
You should always look for a simpler solution when using regular expressions.
source share