I have a string
string= "& & This is&test release & this is rest release &";
Output ="& & This istest release this is rest release &"
Input:
string ="& & This is&test release & this is rest release &";
Output should be
"& & This istest release this is rest release &";
But am getting output is
"& & This istest release & this is rest release &";
My code:
var str = "& & & This is&test release * this is rest release & &"; var str2=""; var str1=str.split(' '); for(var i=0;i<str1.length;i++) { if(/^[0-9a-zA-Z]/.test(str1[i])) { str1[i]=str1[i].replace(/[^a-zA-Z ]/g, "") } str2=str1.join(' '); } console.log(str2);
I am not sure about the white spaces in string here (do you actually need them ,but here's an alternative you can try)
let string = "& & This is&test release & this is rest release &"; let a = string.split("&").map(el => { if (el == "" || el == " ") { el = " &" } return el }) let result = a.join(""); // join acc to gap you want console.log(result)
Try
let s= "& & This is&test release & this is rest release &"; let o= s.replace(/^([& ]*)(.*?)([& ]*)$/, (m,a,b,c)=> a+b.replace(/&/g,'')+c); console.log(o);
Here is something I came up with that will work with any special characters.
var str = "& & This is&test _&&release & this_is rest release &";
var replacedString = str.replace(/(\w\s*)([^a-zA-Z0-9]+)(\s*\w)/g, "$1 $3");
console.log(replacedString);
Output:
& & This is test release this is rest release &
The replace pattern will match any number of special characters between any alphanumeric character.
what about this? it works perfectly. I splitted the string to start, middle and end. then replaced the special characters only in the middle, and then joined all the 3 parts back.
string= "& & This is&test release & this is rest release &"; i=string.search(/[\\w\\s]/i)//first letter j=string.search(/[\\w\\s][^\\w\\s]*$/i)//last letter start=string.substr(0,i) middle= string.substr(i,j-i+1) end=string.substr(j+1) middle=middle.replace(/[^\\w\\s]/gi,"")//replace only in the middle output=start+middle+end console.log(output)
Hoped I helped you.
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.