Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove all the characters after $ sign using Javascript

Tags:

javascript

I want to remove all the characters that appear after "$" sign in my string using javascript.

Is there any function in javascript which can help me achieve this. I am quite new to client side scripting.

Thanks.

like image 474
Janet Avatar asked May 23 '11 20:05

Janet


People also ask

How do you remove all characters from a string after a specific character in JavaScript?

slice() method to remove everything after a specific character, e.g. const removed = str. slice(0, str. indexOf('[')); . The slice method will return the part of the string before the specified character.

How do I remove all characters from a string after a specific character?

Using 'str. replace() , we can replace a specific character. If we want to remove that specific character, replace that character with an empty string. The str. replace() method will replace all occurrences of the specific character mentioned.

How do I remove a character from the end of a string in JavaScript?

To remove the last character from a string in JavaScript, you should use the slice() method. It takes two arguments: the start index and the end index. slice() supports negative indexing, which means that slice(0, -1) is equivalent to slice(0, str. length - 1) .


2 Answers

How about this

astr.split("$")[0];

NB This will get you all of the characters up to the $. If you want that character too you will have to append it to this result.

like image 182
Hogan Avatar answered Oct 06 '22 08:10

Hogan


You can try this regex, it will replace the first occurance of $ and everything after it with a $.

str.replace(/\$.*/, '$');

Input: I have $100
Output: I have $

like image 28
Rocket Hazmat Avatar answered Oct 06 '22 07:10

Rocket Hazmat