I have one string, I want to remove some repeated part from string using ajax or javascritp.
The string is -
1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236
Above string is connect using underscore (_) sign. means above string include 3 string. I want to remove -master=122....
The '-master=' is default but after equal sign(=) number will change. So how to remove '-master=n...' from above string.
var s = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
console.log(s.replace(/-master=\d+/g, ''));
Use a replace function with a greedy /-master=\d+/ regex :
PHP
$input = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
$output = preg_replace('/-master=\d+/', '', $input);
echo $output; // 1-16-15_2-34-33_3-33-23
JS
var input = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
var output = input.replace(/-master=\d+/g, '');
console.log(output); // 1-16-15_2-34-33_3-33-23
Try this:
var str = "1-16-15-master=1232_2-34-33-master=1232_3-33-23-master=1236";
str = str.replace(/-master=/g,'=');
Related
A string representing a currency is to be converted to a number.
For example:
Input : "125.632.454.454.403,51"
Output expected : 125632454454403.51
Currently I am trying:
Trial 1)
a = "125.632.454.454.403,51";
a.replace(/./, '');
Result = "25.632.454.454.403,51"
Trial 2)
a = "125.632.454.454.403,51";
a.replace(/./g, '');
Result = ""
But I expect the replace function to find all the occurrences of "." and replace by "".
Trial 3)
a = "125.632.454.454.403,51";
a.replace(/,/, '');
Result = "125.632.454.454.40351"
I would be glad if I find a fix for this.
You need to use \. instead of .. The dot (.) matches a single character, without caring what that character is. Also you can do it with single replace() with callback .
var str = "125.632.454.454.403,51";
str = str.replace(/\.|,/g, function(m) {
return m == '.' ? '' : '.'
});
document.write(str);
try:
var str = "125.632.454.454.403,51" ;
var result = str.replace(/\./g,'').replace(/\,/g,'.');
console.log(Number(result))
replace returns the changed string, it does not change it in-place!
You can find this out, by refering to the documentation.
Use
var Result = a.split('.').join("");
console.log(Result);
. has specific meaning in a regex. It matches any character. You need to escape the dot if you are actually looking for the character itself
var a = "125.632.454.454.403,51";
var result = a.replace(/\./g,"");
You can also do (parseFloat(a.replace(/[^0-9]+/g,""))/100)
And if you have to do this for multiple currencies, I would recommend looking into autonumeric.js. It handles all this for you.
I have a string that contains something like name="text_field_1[]" and I need to find and replace the '1[]' part so that I can increment like this '2[]', '3[]', etc.
Code:
$search = new RegExp('1[]', 'g');
$replace = $number + '[]';
$html = $html.replace($search, $replace)
You can use \d in your regexp whitch means that onlu numbers used before [].
Also you need to escape [] because of it's special characters in regexp.
$search = new RegExp('\\d+\\[\\]', 'g');
$replace = $number + '[]';
$html = $html.replace($search, $replace)
Code: http://jsfiddle.net/VJYkc/1/
You can use callbacks.
var $counter = 0;
$html = $html.replace(/1\[\]/g, function(){
++$counter;
return $counter+'[]';
});
If you need [] preceded by any number, you can use \d:
var $counter = 0;
$html = $html.replace(/\d\[\]/g, function(){
++$counter;
return $counter+'[]';
});
Note:
escape brackets, because they are special in regex.
be sure that in $html there is only the pattern you need to replace, or it will replace all 1[].
Braces must be escaped within regexps...
var yourString="text-field_1[]";
var match=yourString.match(/(\d+)\[\]/);
yourString=yourString.replace(match[0], parseInt(match[1]++)+"[]");
Here's something fun. You can pass a function into string.replace.
var re = /(\d+)(\[\])/g/;
html = html.replace(re, function(fullMatch, value, braces) {
return (parseInt(value, 10)+1) + braces;
}
Now you can replace multiple instances of #[] in your string.
I have following example:
var VarFull = $('#selectror').attr('href') where .attr('href') = "#tabs1-1"
How can I trim that to "tabs1-1" ( without #)??
Any suggestions much appreciated.
Use substring:
var VarFull = $('#selectror').attr('href').substring(1);
You can use JavaScript's string replace(): https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/String/replace
var VarFull = $('#selectror').attr('href');
var trimmed = VarFull.replace('#','');
Edit:
This is a good article on JS string manipulation: http://www.quirksmode.org/js/strings.html
You could use replace -
var VarFull = $('#selectror').attr('href').replace('#','');
try this regEx with replace-
var VarFull = $('#selectror').attr('href').replace(/\#*/g, "");
it will replace all the # in your attr.
If it's certain that the url will contain # anyway, you can even split and take second element of array.
var trimmed=$('#selectror').attr('href').split("#")[1]
But don't use this if URL may not contain # otherwise you'll get an undefined error for trying to get index 1 of the array by split().
For example: ". / email#gmail.com, / . \"
Now trim characters at the beginning and end of the string:
email_new = email.replace(/\W+$/g, '').replace(/^\W+/g, ''); // output : email#gmail.com
Similar to my previous question:
spliting a string in Javascript
The URLs have now changed and the unique number ID is no longer at the end of the URL like so:
/MarketUpdate/Pricing/9352730/Report
How would i extract the number from this now i cannot use the previous solution?
You could search for
/(\d+)/
and use backreference no. 1 which will contain the number. Note that this requires the number to always be delimited by slashes on both sides. If you also want to match numbers at the end of the string, use
/(\d+)(?:/|$)
In JavaScript:
var myregexp = /\/(\d+)\//;
// var my_other_regexp = /\/(\d+)(?:\/|$)/;
var match = myregexp.exec(subject);
if (match != null) {
result = match[1];
} else {
result = "";
}
If the URLs always look like that, why not use split() ?
var ID = url.split('/')[3];
urlstring = "/MarketUpdate/Pricing/9352730/Report"
$str = urlstring.split("/");
alert($str[3]);
This splits the string each time it finds the / symbol and stores it into an array, You can then get each word in the array by using $str[0]
I would like to extract some text between two points in a string, in Javascript
Say the string is
"start-extractThis-234"
The numbers at the end can be any number, but the hyphens are always present.
Ideally I think capturing between the two hypens should be ok.
I would like the result of the regex to be
extractThis
string = "start-extractThis-234"
console.log( string.match( '-(.*)-' )[1] );
//returns extractThis
why not just do
var toExtract = "start-extractThis-234";
var extracted = null;
var split = toExtract.split("-");
if(split.length === 3){
extracted = split[1];
}
^.+?-(.+?)-\d+$