0

As mentioned on the title here is a div element

<div id="description" class="bold"> 
This is an example text i wanna keep. 
# And this is the text i want to remove.
</div>

I use this jquery code to get the whole content of the div element and store it in a variable:

<script type="text/javascript">
var $itemdesc = $('#description').text();
</script>

but how can next remove the content that start with the # hashtag?

Designer
  • 875
  • 7
  • 26

5 Answers5

1

If the content is always the same structure - ie: text you want + '#' + text you dont want - then simply split the text content on the character and pass only the first portion to a variable.

let content = document.querySelector('#description').textContent.split('#')[0].trim();


console.log(content); // gives "This is an example text i wanna keep."
<div id="description" class="bold"> 
This is an example text i wanna keep. 
# And this is the text i want to remove.
</div>
gavgrif
  • 15,194
  • 2
  • 25
  • 27
  • You code wont work with muliple lines. Check [this answer](https://stackoverflow.com/a/57835331/1248177) – aloisdg Sep 07 '19 at 16:00
1

One way to do it would be to split the text based on the # character and just use the first element of the resultant array.

const text = document.querySelector('#description').innerHTML;
const split = text.split('#')
console.log(split[0].trim());
<div id="description" class="bold"> 
This is an example text i wanna keep. 
# And this is the text i want to remove.
</div>

Another option would be to use regular expressions, like in this example: How to match "anything up until this sequence of characters" in a regular expression?.

Nick
  • 16,066
  • 3
  • 16
  • 32
1

You can do that with simple regex...

var $itemdesc = $('#description').text();

console.log($itemdesc.replace(/^#.*\n/gm, ''));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="description" class="bold"> 
This is an example text i wanna keep. 
# And this is the text i want to remove.
This is an example text i wanna keep. 
# And this is the text i want to remove.
This is an example text i wanna keep. 
# And this is the text i want to remove.
This is an example text i wanna keep. 
# And this is the text i want to remove.
</div>
ManUtopiK
  • 4,495
  • 3
  • 38
  • 52
1

Assuming you want to remove up to the end of the line that contains the #, use replace with this regex #.*\n:

var $itemdesc = $('#description').text();

var $result = $itemdesc.replace(/#.*\n/g, '');

console.log($result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<div id="description" class="bold"> 
This is an example text i wanna keep.
# And this is the text i want to remove.
And keep this as well. 
# And remove this as well.
</div>
DjaouadNM
  • 22,013
  • 4
  • 33
  • 55
0

You achieve this in one line with filter:

  • Split text by newlines
  • Filter every lines starting with anything but '#'
  • Concat back every lines

$('#description').text($('#description').text().split("\n").filter(x => !x.startsWith('#')).join('\n'));
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="description" class="bold"> 
This is an example text i wanna keep. 
# And this is the text i want to remove.
</div>

Same solution without JQuery:

document.querySelector('#description').innerText = document.querySelector('#description').textContent.split("\n").filter(x => !x.startsWith('#')).join('\n');
<div id="description" class="bold"> 
This is an example text i wanna keep. 
# And this is the text i want to remove.
</div>

This code will also work with multi lines:

document.querySelector('#description').innerText = document.querySelector('#description').textContent.split("\n").filter(x => !x.startsWith('#')).join('\n');
<div id="description" class="bold"> 
This is an example text i wanna keep. 
# And this is the text i want to remove.
This is an example text i wanna keep. 
# And this is the text i want to remove.
</div>
aloisdg
  • 22,270
  • 6
  • 85
  • 105
  • This is an interesting solution, but seems inefficient, especially if there are a lot of words you have to iterate over! – Nick Sep 07 '19 at 16:04