How do i remove a text before the first occurrence of "_" from a string using javascript/typescript?

How would I remove the text appearing before the first occurrence of the "_"?

var string = "apple_ball_cat_Dog";
var newString = string.split("_").pop();
console.log(newString);

Actual result: Dog

Expected result: ball_cat_Dog

Please try this.

var string = 'apple_ball_cat_Dog';
var newString = string.substring(string.indexOf("_") + 1, string.length);
console.log(newString);

Select the cells you will remove texts before or after a specific character, press Ctrl+ Hkeys to open the Find and Replacedialog. In the Find and Replace dialog box: 1. For removing all before the specific character such as comma, please type*,into the Find whattext box; 2.

you have wrap the string inside quotes like as use substring() and indexOf() function:

var string = "apple_ball_cat_Dog";
var result = string.substring(string.indexOf("_") + 1, string.length);
console.log(result);

You can use the FIND function to get the position of the first occurrence of the comma character in a text in B1, then using the LEFT function to extract the left-most characters before the first comma. So you can remove the text after the first comma character in text. You can use the following formula: = LEFT (B1, FIND (",",B1)-1)

var string = 'apple_ball_cat_Dog';
var newString = string.split('_');
newString.shift();
newString = newString.join('_');
console.log(newString);

1. Select the range that you will remove all characters after the first space from each cell, and click Kutools > Text > Split Names. See screenshot: 2. In the opening Split Names dialog box, only check the First Name option, and click the Ok button. 3.

The way I would do it is use the search string method (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/search) to find the index of the first occurence of _, and then slice the string on this index + 1, to get only what is after the found index (https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/slice)

var string = 'apple_ball_cat_Dog';
var index = string.search('_');
var result = string.slice(index + 1);

console.log(result);

Since this is a simple string manipulation, you can use the following to remove all characters before the first comma: $string = preg_replace('/^ [^,]*,\s*/', '', $input); preg_replace () allows you to replace parts of a string based on a regular expression. Let's take a look at the regular expression.

Try This

var string = 'apple_ball_cat_Dog';
var n = string.split('_');
n.shift();
var new_string = n.length ? n.join('_') : string;
console.log(new_string);

(1) If you want to extract text before or after comma, you can change " "to ",". (2) If you want to extract the text after space only, use this formula =MID(A1,FIND(" ",A1)+1,256). (3) This method extracts text by the first space in specified cells.

Extract text after the last instance of a specific character. If you have a list of complex text strings that contain several delimiters (take the below screenshot as example, which contains hyphens, comma, spaces within a cell data), and now, you want to find the position of the last occurrence of the hyphen, and then extract the substring after it.

To extract text before a special character, you need to find the location of the special character in the text, then use Left Function. Formula: Copy the formula and replace "A1" with the cell name that contains the text you would like to extract. =LEFT(A1, FIND(".",A1)-1) Example: To extract characters before special character "."

The * after the colon means any number of the things right before me (in this case the not-colon). Finally, the : selects the colon. In other words, select the beginning of the line, any number of things that aren't a colon, and the first colon. The //g means delete every matched instance.

Comments
  • Actual actual result: VM67:1 Uncaught ReferenceError: apple_ball_cat_Dog is not defined