I would like to modify my strings so i can replace the character ' ' with '_' using JS,for example "new zeland"=>"new_zeland"
how can i do that?
var str = 'new zealand';
str = str.replace(/\s+/g, '_');
You could use Rob's code, but it uses a regular expression to find the space, while it would be faster to just search for a literal space:
var string = 'new zealand';
var newString = string.replace(' ', '_');
Related
I have some string that looks like this:
var string = popupLink(25, 'Any string')
I need to use a regular expression to change the number inside (note that this is a string inside of a larger string so I can't simply match and replace the number, it needs to match the full pattern, this is what I have so far:
var re = new RegExp(`popupLink\(${replace},\)`, 'g');
var replacement = `popupLink(${formFieldInsert.insertId},)`;
string = string.replace(re, replacement);
I can't figure out how to do the wildcard that will maintain the 'Any String' part inside of the Regular Expression.
If you are looking for a number, you should use \d. This will match all numbers.
For any string, you can use lazy searching (.*?), this will match any character until the next character is found.
In your replacement, you can use $1 to use the value of the first group between ( and ), so you don't lose the 'any string' value.
Now, you can simply do the following:
var newNumber = 15;
var newString = "var string = popupLink(25, 'Any string')".replace(/popupLink\(\d+, '(.*?)'\)/, "popupLink(" + newNumber + ", '$1')");
console.log(newString);
If you just need to change the number, just change the number:
string = string.replace(/popupLink\(\d+/, "popupLink(" + replacement);
Example:
var str = "var string = popupLink(25, 'Any string')";
var replacement = 42;
str = str.replace(/popupLink\(\d+/, "popupLink(" + replacement);
console.log(str);
If you really do have to match the full pattern, and "Any String" can literally be any string, it's much, much more work because you have to allow for quoted quotes, ) within quotes, etc. I don't think just a single JavaScript regex can do it, because of the nesting.
If we could assume no ) within the "Any String", then it's easy; we just look for a span of any character other than ) after the number:
str = str.replace(/(popupLink\()\d+([^)]*\))/, "$1" + replacement + "$2");
Example:
var str = "var string = popupLink(25, 'Any string')";
var replacement = 42;
str = str.replace(/(popupLink\()\d+([^)]*\))/, "$1" + replacement + "$2");
console.log(str);
If I have a input value "a[123],b[456],c[789]" and I want to return as "a=123&b=456&c789"
I've tried below code but no luck.. Is there a correct way to implement this?
var str = "a[123],b[456],c[789]"
var string = (str).split(/\[|,|\]/);
alert(string);
One option is:
var rep = { '[': '=', ']': '', ',': '&' };
var query = str.replace(/[[,\]]/g, el => rep[el] );
The delimiters are already there, it's just a matter of replacing one delimiter with another. Replace each [ with an =, replace each , with an &, and remove all ].
var str = "a[123],b[456],c[789]"
var string = str.replace(/([a-z])\[(\d+)],?/g, '$1=$2&').slice(0, -1);
alert(string);
Brute force way im not good at Regex. Just adding my thoughts
var str = "a[123],b[456],c[789]"
str = str.replace(/],/g, '&');
str = str.replace(/\[/g, '=');
str = str.replace(/]/g,'');
alert(str);
The simple 2 line answer for this is:
str=str.replace(/,/g,"&");
str=str.replace(/(\w)\[(\d+)\]/g,"$1=$2");
I have a string "#{Name; 11112121#xyz.com}"
I want to write a regular expression that extracts Name and 11112121 from the above string
This is what I tried.
function formatName(text){
var regex = /#\{([^;]+); ([^\}]+)\}/
return text.replace(
regex,
'$1, $2'
);
}
The above gives Name, 11112121#xyz.com. But I want only Name, 11112121
try this
var regex = /#\{([^;]+); ([^\}]+)(#.*)\}/
$1 => Name
$2=> 11112121
Here is the working example
Use match instead, like this:
var regex = /#\{([^;]+);\s+([^#]+)/;
var matches = text.match(regex);
alert(matches[1] + ', ' + matches[2]);
http://jsfiddle.net/rooseve/bM2U6/
If you want to match everything until the # character, you can use
var regex = /#\{([^;]+); ([^#]+)/
If you need to verify that the string also contains a } after that, you can add that to the end:
var regex = /#\{([^;]+); ([^#]+)[^}]*\}/
I'm able to use .replace(/\+/g, ' '); jQuery method.
But I need to use it with dynamic parameters.
Example:
var str1 = 'aaa';
$('#myelement').val().replace(/str1\/g,' ');
How can I do that ?
To start with string.replace() is Javascript and not jQuery. for more info check out String.replace on MDN.
Also string.replace() returns a new string with the replacements and does not affect the string it's run against.
You can create a new RegExp with any arbitrary pattern
var regex = new RegExp('aaa', 'g');
then do:
var replacedString = $('#myelement').val().replace(regex,' ');
to get the string with the substitutions.
So to replace the content you'll have to do:
$('#myelement').val($('#myelement').val().replace(regex,' '));
First of $('#myelement') will give you a jQuery object, you must replace the string inside its html or value. Try this:
var str1 = 'aaa';
var reg = new RegExp(str1,"g");
$('#myelement').val($('#myelement').val().replace(reg, ' '));
$('#myelement').val($('#myelement').val().replace(/str1\/g,' '));
Try this
var pattern = /(aaa)\b/g;
$('#myelement').val().replace(pattern,' ');
I have a string and I need to replace all the ' and etc to their proper value
I am using
var replace = str.replace(new RegExp("[']", "g"), "'");
To do so, but the problem is it seems to be replacing ' for each character (so for example, ' becomes '''''
Any help?
Use this:
var str = str.replace(/'/g, "'");
['] is a character class. It means any of the characters inside of the braces.
This is why your /[']/ regex replaces every single char of ' by the replacement string.
If you want to use new RegExp instead of a regex literal:
var str = str.replace(new RegExp(''', 'g'), "'");
This has no benefit, except if you want to generate regexps at runtime.
Take out the brackets, which makes a character class (any characters inside it match):
var replace = str.replace(new RegExp("'", "g"), "'");
or even better, use a literal:
var replace = str.replace(/'/g, "'");
Edit: See this question on how to escape HTML: How to unescape html in javascript?
Rather than using a bunch of regex replaces for this, I would do something like this and let the browser take care of the decoding for you:
function HtmlDecode(s) {
var el = document.createElement("div");
el.innerHTML = s;
return el.innerText || el.textContent;
}