开发者

How to remove Bullets from the text using javascript regular expression

开发者 https://www.devze.com 2023-03-15 16:54 出处:网络
I am just trying to remove bullets from the text. For example when i am copying bulleted text list from MS Word to any textbox 开发者_如何学编程it is showing along with bullet. Can somebody tell me ho

I am just trying to remove bullets from the text. For example when i am copying bulleted text list from MS Word to any textbox 开发者_如何学编程it is showing along with bullet. Can somebody tell me how to remove any type of bullet and replaces it with "".

I have found this code from different sources, But they are not working for me.

 var x="  1.     15-20 years  ";
 x.replace('•\t.+', '');
 alert(x); // i want out put as 15-20 years

Thank you. :-)


Try this statement instead...

x.replace(/[•\t.+]/g, '');


I think this fits your needs. http://jsfiddle.net/ksSG8/

 var x="  1.     15-20 years  ";
 x = x.replace(/\s\d\.\s*/, '');
 alert(x);

One part that is missing from many answers and your code is:

x = x.replace(...);

x never receives the value returned from the replace() function if you do not assign it back to x.


Do you see any • in x? No, so you can't replace it. To achieve what you want, use:

x.replace(/^\s*[0-9]+\.\s*/, '');

How to remove Bullets from the text using javascript regular expression

What the regex does is basically removing any [number]., along with any whitespace before and after it, so what is left is the text you need.


x= x.replace(/^\s*\d+\.\s*/, ''); // strings are immutable


This solution finds both bulleted and numbered items at the beginning of the line of text, then removes them.

var x="  1.     15-20 years  ";
x.replace(/^\s*(?:[••••]|\d+)\.\t/, '');
alert(x); // i want out put as 15-20 years

I think you are trying to replace a substring, instead of replacing with a regular expression.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号