I have a Address like
Address : 273A-84, Sundharam Street,, Ganthi Path, Kovai,,,,India,641001
If string has more than 2 commas i want to replace with single comma.
In above address next to street and next to kovai there is multiple commas.I want to replace with single comma.
My expected output is:
Address : 273A-84, Sundharam Street,Ganthi Path,Kovai,India,641001
Please suggest regular expression.
var str = "Address : **273A-84, Sundharam Street,, Ganthi Path, Kovai,,,,India,641001**";
str.replace(/\,{1,}/gi, ',');
DEMO:http://jsbin.com/sejuma/2/
You can try this:-
var str="Address : 273A-84, Sundharam Street,, Ganthi Path, Kovai,,,,India,641001 ";
res = str.replace(/^[, ]+|[, ]+$|[, ]+/g, ",").trim();
alert("{" + res+ "}");
Related
I am trying to create a regex that removes all text after a comma and the space+word after the comma.
So if I have the string:
"123 some address, place 2800 Australia"
I want to remove " 2800 Australia". The word "place" can be anything so the regex needs to match a word not the specified "place" string.
So far I have this as working best:
var s = '123 some address, place 2800 Australia';
s = s.substring(0, s.indexOf(', '));
document.write(s);
However this will of course also remove "place" and I need the first word after the comma to still be included.
Thanks!
You can do this by using split(), replace(), match() function with some ReGex - This was a bit tricky as i had to consider two commas scenarios as well.
First we can use match the commas in the string get the address only then you can use the get the place and split from the space " "
After that we can consider two commas scenario which will be after the place so we can check by using replace() and replace with nothing.
Lastly its not possible with using jQuery as jQuery is Javascript.
Having one comma
var text = '123 some address, place 2800 Australia';
//Match and get Address
var addr = text.match(/([^,]*)(.*)/)[1];
//Match and get place with split
var place = text.match(/([^,]*),(.*)/)[2].split(' ')[1]
//New Address
var newAdd = addr + ', ' + place
//Write
document.write(newAdd);
//Console.log
console.log(newAdd);
Having two commas (possibility)
The solution works for two commas as well.
var text = '123 some address, place, 2800 Australia';
//Match and get Address
var addr = text.match(/([^,]*)(.*)/)[1];
//Match and get place with split
var place = text.match(/([^,]*),(.*)/)[2].split(' ')[1]
//If two commas
var ifTwoCommas = place.replace(',', '');
//New Address
var newAdd = addr + ', ' + ifTwoCommas
//Write
document.write(newAdd);
//Console.log
console.log(newAdd);
I am recieving strings like:
"\\r\\n "
"\\r \\n"
"\\n \\r "
etc from a third party API.
I need to convert these to empty string before storing them into DB. How can I properly sanitize these in Node.js?
I can use .replace but I want to make sure to catch all edge cases.
Considering you just want to replace the string with no values other than space character class or escaped space characters.
let str = `"\\r\\n "
"\\r \\n"
"\\n \\r "
"\\n \\r Hello"
`
let op = str.replace(/^"(?:\\r|\\n|\s)*"?$/gm,'')
console.log(op)
Try this :
console.log("\\n \\r ".replace(/\\(n|r)\s*/g,""));
Try ( i use \\\\r for slashesh as escape characters in JS to poperly generate your input string - which is shown console)
let input=" \\\\r \\\\n ";
let output= input.replace(/\s*(\\\\r|\\\\n)\s*/g, '');
console.log(`input : "${input}"` );
console.log(`output : "${output}"`);
Hi I have a text something like
"Welcome back ##Firstname ##Lastname. You Last accessed on ##Date"
My objective is to replace these tokens with actual values.
So what i did was
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date;
var data = str.split('#');
My idea was - once i do this, my data will have an array of values something like
["Welcome back", "#FirstName" , "#LastName", "You Last accessed on" , "#Date"]
Once i have this, i can easily replace the tokens because i will know which one are properties and which one are static string. But fool i am since JS has other ideas.
it instead split it as :
["Welcome back ", "", "Firstname ", "", "Lastname. You Last accessed on ", "", "Date"]
What am i doing wrong? or what is the best way to replace tokens in a string?
I looked here. Did not like the approach much. Not a fan of curly brackets. would like to do it the "#" way - Since it will be easy for Content authors
Another regex option, split on /#(#\w+)[^\w#]+/, captures the name part while throwing off the first #, assuming the name identifiers are always made up of word characters:
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date;"
var data = str.split(/#(#\w+)[^\w#]+/);
console.log(data.filter(s => s !== ""));
You can split "#" characters which are followed by "#" characters
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date";
var res = str.split(/#(?=#)/);
console.log(res);
you can replace the '##' by some character followed by '#' like ',#' and then replace on that new character.
var str = "Welcome back ##Firstname ##Lastname. You Last accessed on ##Date";
var data = str.replace(/##/g, ',#').split(',');
console.log(data);
As your delemiters end with a space, may split by space:
.split(" ")
And then iterate and replace all words beginning with ##
var replaceBy={
lastname:"Doe",
name:"John"
}
var result= input.split(" ").map(function(word){
if(word[0]=="#" && word[1]=="#"){
return replaceBy[word.substr(2)] || "error";
}
return word:
}).join(" ");
However, it might be easier to suround your identifiers with delemiters e.g.:
Hi ##lastname##!
So you can do
.split("##")
And every second element is automatically an identifier.
Do this:
data = str.replace("##Firstname", "Robert").replace("##Lastname","Polson").replace('##Date','yesterday') ;
Example of text:
Some string here : my value
Another string : my value
String : my value
I want to match everything before and including the symbol :
My wanted output is:
Some string here :
Another string :
String :
Thanks
Just use:
(.* :)
See example: https://regex101.com/r/bA1cQ1/2
Don't use a regular expression, because it's not a nail to regex's hammer.
var strToMatch = "Some string here : my value";
var match = strToMatch.slice(0,strToMatch.indexOf(':')+1);
// do something with the match
document.body.appendChild(document.createElement('pre')).innerHTML = match;
I have this String :
var test = "toto#test.com";
I would like to replace all character after the "#" character by empty value.
I would like to get this String :
var test = "toto"
Try this:
test= test.split('#')[0]
"toto#test.com".replace(/#.+$/, '')