简体   繁体   中英

Remove string after character in every line break

Hi I'm a newbie at javascript and I have some text that contains multiple lines.I want to remove strings after the character '(' in every line breaks and then replace all line breaks with commas. This is what I have so far

var text = "black gloves (?) 36622\nred eyes (?) 587624\nsolo (?) 1975043\nshort hair (?) 1628547"

var draft = text.replace(/(\r\n|\n|\r)/gm,",");
var final = draft.split('(')[0];
alert(final);

I can't seem to get my code to give me this result:

black gloves,red eyes,solo,short hair

but instead it just gives black gloves

Try this:

 var text = "black gloves (?) 36622\\nred eyes (?) 587624\\nsolo (?) 1975043\\nshort hair (?) 1628547" var draft = text.replace(/(\\r\\n|\\n|\\r)/gm," "); var draft = draft.replace(/(\\d)+/gm,","); var draft = draft.replace(/\\(\\?\\)/gm,""); alert(draft); 

OR

 var text = "black gloves (?) 36622\\nred eyes (?) 587624\\nsolo (?) 1975043\\nshort hair (?) 1628547" var draft = text.replace(/\\(\\?\\)[ \\d\\r\\n]+/gm,","); alert(draft); 

You must know that String.prototype.split() returns an array and in this case it's not necessary to use it.

You just need to use String.prototype.replace() method to achieve what you want, as follows:

 var text = "black gloves (?) 36622\\nred eyes (?) 587624\\nsolo (?) 1975043\\nshort hair (?) 1628547"; var draft = text.replace(/(\\r\\n|\\n|\\r)/gm, ",").replace(/[^a-zA-Z,]/g, " ").replace(/\\s+,/g, ",").trim(); console.log(draft); 

Here it goes https://jsfiddle.net/loginshivam/724phqag/

var text = "black gloves (?) 36622\nred eyes (?) 587624\nsolo (?) 1975043\nshort hair (?) 1628547"

var array = text.split("\n");
var val;
for(in=0;in<array.length;in++){
    val=array[in];
    if(in == 0)
       text = val.substring(0, val.indexOf("(")) ;
    else
    text = text  + ","+ val.substring(0, val.indexOf("("));
}

Without the regex:

 var text = "black gloves (?) 36622\\nred eyes (?) 587624\\nsolo (?) 1975043\\nshort hair (?) 1628547" var textToArray = text.split('\\n'); for(var i = 0; i<textToArray.length; i++) { var index = textToArray[i].indexOf('(') - 1; textToArray[i] = textToArray[i].slice(0,index); } var finalString = textToArray.join(', '); alert(finalString); 

text.replace(/ \(.\) \d+/g, '').replace(/\r?\n/g, ',')

Just as simple as one line two replaces.

 var text = "black gloves (?) 36622\\nred eyes (?) 587624\\nsolo (?) 1975043\\nshort hair (?) 1628547"; var final = text.replace(/ \\(.\\) \\d+/g, '').replace(/\\r?\\n/g, ','); alert(final); 

An alternate regex solution ( https://regex101.com/ is very helpful). It's a bit brittle in that it assumes each line is properly formatted and contains the pattern.

 var text = "black gloves (?) 36622\\nred eyes (?) 587624\\nsolo (?) 1975043\\nshort hair (?) 1628547" console.log(text.replace(/ \\(\\?\\) .*(\\n|$)/g, ",").slice(0,-1)); 

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