Q:

javascript extract ..stop at the first underscore in the value

Trying to extract hostname-rt45 from hostname-rt45_34_we_35. I am using /(.)_?./g. This doesn't seem to work. I have reviewed the regex documentation. Thought using the ? would make it greedy and stop at the first underscore. What am I missing?
at https://regexr.com/, I have tried. 
/(.*)_?.*/g
/(.*)_.*/g

A:

You can look for a sequence of non _ characters that have an underscore after them (positive lookahead) - (RegExr example):

const regex = /[^_]+(?=_)/;
const str = 'hostname-rt45_34_we_35';

const [result] = str.match(regex);

console.log(result);

