简体   繁体   中英

Remove all characters before “-” on each line of multiline textarea text

I am getting some text from a multiline textarea, each line is separated by a line break:

 1 - test1
 2 - test2
 3 - test3
 4 - test4

How can I remove on each line all text before the - , so the text will look like:

 test1
 test2
 test3
 test4

You could look for no dashes and a dash and replace with an empty string for multiline data.

 var string = ' 1 - test1\\n 2 - test2\\n 3 - test3\\n 4 - test4', result = string.replace(/^[^-]+-/gm, ''); console.log(result); 

You can do it using split and splice.

 const mytext = ' 1 - test1'; const arr = mytext.split('-'); const splice = arr.splice(1,1 ,''); const final = splice.toString(); console.log(final); 

this could be solved with a regular expression. Try to put down your string to check if it is possible.

here is the good website for it: https://regex101.com/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM