const regex = /(?<!\d)(?<!_)(?<![a-zA-z])string(?!\d)(?!_)(?![a-zA-Z])/gm;
// Alternative syntax using RegExp constructor
// const regex = new RegExp('(?<!\\d)(?<!_)(?<![a-zA-z])string(?!\\d)(?!_)(?![a-zA-Z])', 'gm')
const str = `module mpi_hello_world
use mpi
implicit none
contains
subroutine hello_world(size, & ! testtest
& r_ank, &!
& myhostname &
& )
! this is a comment
! this also
integer, intent(in) :: size, rank23, rank_er,ranking, myrank, my2r_ank
character(len = *), intent(out) :: myhostname
integer :: stat
myhostname = "(no host name)"
#ifdef __INTEL_COMPILER
call get_environment_variable("COMPUTERNAME", myhostname)
#else
stat = hostnm(myhostname)
#endif
write(*, *) "Hello world FORTRAN rank:", rank, " size:", size, &
& "hostname:", trim(myhostname)
end subroutine hello_world
end module mpi_hello_world
`;
// Reset `lastIndex` if this regex is defined globally
// regex.lastIndex = 0;
let m;
while ((m = regex.exec(str)) !== null) {
// This is necessary to avoid infinite loops with zero-width matches
if (m.index === regex.lastIndex) {
regex.lastIndex++;
}
// The result can be accessed through the `m`-variable.
m.forEach((match, groupIndex) => {
console.log(`Found match, group ${groupIndex}: ${match}`);
});
}
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for JavaScript, please visit: https://developer.mozilla.org/en/docs/Web/JavaScript/Guide/Regular_Expressions