JavaScript : Replace all character by empty after a specific character - javascript

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

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);

Extract a value from json inside json string

var a = '{"test_cmd": "pybot -args : {\"custom-suite-name\" : \"my-1556\"}}'
b = a.match("\"custom-suite-name\" : \"([a-zA-Z0-9_\"]+)\"")
console.log(b)
I have a json string inside json string.
I like to extract the property custom-suite-name's value using the regex.
Output must be,
my-1556.
But the return value b has no matches/null value.
Don't use a regexp to parse JSON, use JSON.parse.
var obj = JSON.parse(a);
var test_cmd = obj.test_cmd;
var args_json = test_cmd.replace('pybot -args : ', '');
var args = JSON.parse(args_json);
var custom_suite = args.custom_suite_name;
It's better to put your regex within / slashes. NOte that,
There is a space exists before colon.
No need to escape double quotes.
Include - inside the character class because your value part contain hyphen also.
Code:
> a.match(/"custom-suite-name" : "([a-zA-Z0-9_-]+)"/)[1]
'my-1556'
> a.match(/"custom-suite-name"\s*:\s*"([a-zA-Z0-9_-]+)"/)[1]
'my-1556'

what regex match strings of characters before ":"

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;

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\\)\\{(.*)\\}";

Regex/Replace any chars inside match combination

For example
var string = 'width="300" height="650"'
i would like to update height in that string and get something like
string = string.replace(/height="..."/g, 'height="150"');
//... as any symbol
how to make reg expression who does not care value of height, to replace it with new one?
You can play with it here, in example:
JsFiddle
var string = 'width="300" height="650"';
string = string.replace(new RegExp(/height=\"[0-9]+\"/g), 'height="150"');
DEMO
for case insensitive use:
.replace(new RegExp(/height=\"[0-9]+\"/gi), 'height="150"');

Categories