Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to replace underscores with spaces?

I have an array with objects inside of it, a few of the objects contain an underscore in the string.

Example:

{"name": "My_name"} 

But I'm calling the name function in multiple places, one such place is in an image tag where the underscore is necessary, using JavaScript I want to select a certain div with the name in it and replace the underscore with space.

Example:

<div>  <div class="name">   My_name  </div>  <img src="My_name.jpg"/> </div> 

In the div.name I want it to say My name instead of My_name.

like image 445
PhazingAzrael Avatar asked Aug 04 '12 17:08

PhazingAzrael


People also ask

How do you change underscore in Excel with spaces?

Press Ctrl + H to display the Find and Replace dialog box. You can also click the Home tab in the Ribbon and select Replace in the Find & Select group. In the Find what box, type a space. In the Replace with box, type an underscore, dash, or other value.

How do I change underscore with spaces in Python?

To replace underscores with spaces in Python:Use the str. split() method to split the string on each underscore. Call the join() method on a string containing a space. The join method will join the words with a space separator.

How do you get rid of underscore in Python?

You can use the string lstrip() function to remove leading underscores from a string in Python. The lstrip() function is used to remove characters from the start of the string and by default removes leading whitespace characters.


1 Answers

You can replace all underscores in a string with a space like so:

str.replace(/_/g, ' '); 

So just do that before the content is put in. If you need to perform the replacement afterwards, loop using each:

$('.name').each(function () {     this.textContent = this.textContent.replace(/_/g, ' '); }); 
like image 149
Ry- Avatar answered Sep 23 '22 15:09

Ry-