正则表达式,使用jQuery删除字符串中的最后一个特定字符

时间:2022-08-09 00:24:29

I want to continue one task because my head has some problem with a regular expression. How can it be done with a regular expression and jQuery?

我想继续执行一项任务,因为我的头部对正则表达式有一些问题。如何使用正则表达式和jQuery?

I have this HTML:

我有这个HTML:

<div class="test">&nbsp;&gt;&nbsp;&nbsp;&gt;&nbsp;Presentation Text   </div>

And I want to get just this text from all this string, every time at the beginning I have

而且我希望每次开始时都能从这个字符串中得到这个文本

" &nbsp;&gt;&nbsp;&nbsp;&gt;&nbsp;" + Some text ... these special character are not changing every time at the begin .

“ >  > ” +有些文字......这些特殊字符在每次开始时都不会改变。

One way is:

一种方法是:

$('.test').html($('.test').html().replace(/&[^;]+;/g, ''));

It works, but I realized that I have at the end some bad characters and I have to remove "\n\n\n" as well

它有效,但我意识到我最后有一些不好的角色,我必须删除“\ n \ n \ n”以及

My output:

Presentation Text\n\n\n

But I need the clean text without "\n" or something else, just "Presentation Text"

但我需要没有“\ n”或其他东西的干净文本,只需要“演示文稿”

What can I do to solve this problem?

我该怎么做才能解决这个问题?

2 个解决方案

#1


1  

Since you're using jQuery, you can use $.trim to remove white-spaces from the beginning and end of your string.

由于您使用的是jQuery,因此您可以使用$ .trim从字符串的开头和结尾删除空格。

#2


1  

$('.test').html($('.test').html().replace(/&[^;]+;|\s+$/g, ''));

This will remove all the whitespace characters (\n, \t, space etc) from the end of the text.

这将删除文本末尾的所有空白字符(\ n,\ t,空格等)。

#1


1  

Since you're using jQuery, you can use $.trim to remove white-spaces from the beginning and end of your string.

由于您使用的是jQuery,因此您可以使用$ .trim从字符串的开头和结尾删除空格。

#2


1  

$('.test').html($('.test').html().replace(/&[^;]+;|\s+$/g, ''));

This will remove all the whitespace characters (\n, \t, space etc) from the end of the text.

这将删除文本末尾的所有空白字符(\ n,\ t,空格等)。