what regex match strings of characters before ":" - javascript

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;

Related

Remove currency symbol from price

The price are render like this on my website : 20$US , how can I remove the US symbol and keep the $ symbol with regex (JavaScrip) ?
I would like the price to be render like this :20$
I have tried this :
<script>
$.each($('.price'), function() {
var pri = $(this).html();
$(this).html(pri.replace(/\D/g,''));
} )
</script>
Any idea ?
You should use replace method which accepts as first parameter a regex expression.
The replace() method returns a new string with some or all matches of
a pattern replaced by a replacement. The pattern can be a string or a
RegExp, and the replacement can be a string or a function to be called
for each match.
let string='20$US';
let desired = string.replace(/US/gi, '');
console.log(desired);

Not to match a pattern from a string

I am having the following string,
var str = "/\S\w\djoseph/";
I just wanted to fetch the characters that are not in the following pattern,
/\\(\w|\d)/
I mean I just want to extract joseph from the above string. I have tried with the following but my regex is not working as expected.
var str = "/\S\w\djoseph/";
var mat = /[^\\(\w|\d)]/g.exec(str);
console.log(mat); //["/"]
Can anyone help me to get the required string from the target string of mine?
You can use this regex in .replace with a callback:
/\S\whello\s\d/.source.replace(/(\\[wsd])|(.)/g, function($0, $1, $2){
return ($1 == undefined ? "" : $1) + ($2 != undefined ? $2.toUpperCase() : "");
})
//=> "\S\wHELLO\s\d"
This will uppercase anything that is not \w or \s or \d.
How about replacing everything that matches \. with the empty string?
var a = '\\a\\btest\\c\\d';
var result = a.replace(/\\./g, '');
console.log(result);

Remove commas from a string using js

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+ "}");

JavaScript : Replace all character by empty after a specific character

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(/#.+$/, '')

JavaScript Regex return null

I have a string that look like this :
blablablablafunction tr(b){b=b.split("");b=b.reverse();b=b.slice(2);return b.join("")}blablablabla
And i want to get : b=b.split("");b=b.reverse();b=b.slice(2);return b.join("")
with Regex :
var match = "function tr(b){(.*)}";
var f = html.match(match);
And i get null in f.Any idea what is the problem?
You will have to escape special characters in the regex in this case I believe these are { , } and also ( and )(around the function argument list). Use the escape character(\) to do that. So try this regex:
var match = "function tr\\(b\\)\\{(.*)\\}";

Categories