How to remove Bullets from the text using javascript regular expression

前端 未结 5 1377
轻奢々
轻奢々 2021-01-22 10:09

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

相关标签:
5条回答
  • 2021-01-22 10:55

    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 讨论(0)
  • 2021-01-22 10:56
    x= x.replace(/^\s*\d+\.\s*/, ''); // strings are immutable
    
    0 讨论(0)
  • 2021-01-22 10:57

    Try this statement instead...

    x.replace(/[•\t.+]/g, '');
    
    0 讨论(0)
  • 2021-01-22 10:59

    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*/, '');
    

    enter image description here

    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.

    0 讨论(0)
  • 2021-01-22 11:04

    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.

    0 讨论(0)
提交回复
热议问题