RegEx to replace char which shouldnt start and end with some char!! for both python and ?JavaScript


Python:

UseCase: Replace single occurrence of "_" which shouldn’t start and ends with "_".

mystr = "This_is_Something____funny__but_important" 
 
re.sub(r'(?<!_)(_(?!_))', '#', mystr) 
 
'This#is#Something____funny__but#important'
 
Note: If you want to modify for different char sets(for beginners)! replace below keywords
chars you want to play with.
 
(?<!CharShouldntStartWith)(CharSearchingFor(?!CharShouldntEndWith))


 JavaScript:

var mystr = "This_is_Something____funny__but_important" 

mystr.replace(/([^_:])_(?!_)/g, '$1#'); 

"This#is#Something____funny__but#important"


Same here:
/([^CharShouldntStartWith:])CharSearchingFor(?!CharShouldntEndWith)/g



This is not the post to learn about python/java script.
Just a useful find for my pal abby Sheik. :)