Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove unwanted part of String. Regex / JS

If I have a string which looks like this:

var myString = '73gf9y::-8auhTHIS_IS_WHAT_I_WANT'

What regex would I need to end up with:

'THIS_IS_WHAT_I_WANT'

The first part of my String will always be a random assortment of characters. Is there some regex which will remove everything up to THIS?

like image 415
Daft Avatar asked Feb 05 '15 14:02

Daft


People also ask

How do I remove part of a string?

We can remove part of the string using REPLACE() function. We can use this function if we know the exact character of the string to remove. REMOVE(): This function replaces all occurrences of a substring within a new substring.

How do I remove a specific character from a string in regex?

If you are having a string with special characters and want's to remove/replace them then you can use regex for that. Use this code: Regex. Replace(your String, @"[^0-9a-zA-Z]+", "")

How do I remove something from a string in JavaScript?

JavaScript String replace() The replace() method is one of the most commonly used techniques to remove the character from a string in javascript. The replace() method takes two parameters, the first of which is the character to be replaced and the second of which is the character to replace it with.


1 Answers

So you want to strip out everything from beginning to the first uppercase letter?

console.log(myString.replace(/^[^A-Z]+/,""));

THIS_IS_WHAT_I_WANT

See fiddle, well I'm not sure if that is what you want :)


To strip out everything from start to the first occuring uppercase string, that's followed by _ try:

myString.replace(/^.*?(?=[A-Z]+_)/,"");

This uses a lookahead. See Test at regex101;

like image 154
Jonny 5 Avatar answered Oct 18 '22 16:10

Jonny 5