How to remove periods in a string using jQuery - javascript

I have the string R.E.M. and I need to make it REM
So far, I have:
$('#request_artist').val().replace(".", "");
...but I get RE.M.
Any ideas?

The first argument to replace() is usually a regular expression.
Use the global modifier:
$('#request_artist').val().replace(/\./g, "");
replace() at MDC

You could pass a regular expression to the replace method and indicate that it should replace all occurrences like this: $('#request_artist').val().replace(/\./g, '');

The method used to replace the string is not recursive, meaning once it found a matching char or string, it stop looking. U should use a regular expression replace. $("#request_artist").val().replace(/\./g, ''); Check out Javascript replace tutorial for more info.

Related

javascript replace function only replaces the first occurance

document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace("'","%%");
The above statement replaces only the first occurrence of the single quote. Could it be because that I do a submit right after that, and that javascript doesn't wait for the previous statement to be completed before moving on to the next one?
Try using regex /g
.replace(/'/g,"%%")
Change your code as below,
document.getElementById("Message").innerHTML =
document.getElementById("Message")
.innerHTML
.replace(/'/g,"%%");
To replace globally in javascript you need to add /g to the replacement string.
See this SO link How to replace all dots in a string using JavaScript
You should use regular expression in replace.
document.getElementById("Message").innerHTML=document.getElementById("Message").innerHTML.replace(/'/g,"%%");
jsfiddle
Use the Global (g) flag in the replace() parameters.
See here
You should use a regular expression and the /g (global) flag. This will replace all occurrences:
document.getElementById("Message").innerHTML=
document.getElementById("Message").innerHTML.replace(/'/g,"%%");
http://www.w3schools.com/jsref/jsref_replace.asp

Javascript function to remove leading dot

I have a javascript string which have a leading dot. I want to remove the leading dot using javascript replace function. I tried the following code.
var a = '.2.98»';
document.write(a.replace('/^(\.+)(.+)/',"$2"));
But this is not working. Any Idea?
The following replaces a dot in the beginning of a string with an empty string leaving the rest of the string untouched:
a.replace(/^\./, "")
Don't do regexes if you don't need to.
A simple charAt() and a substring() or a substr() (only if charAt(0) is .) will be enough.
Resources:
developer.mozilla.org: charAt()
developer.mozilla.org: substring()
developer.mozilla.org: substr()
Your regex is wrong.
var a = '.2.98»';
document.write(a.replace('/^\.(.+)/',"$1"));
You tried to match (.+) to the leading dot, but that doesn't work, you want \. instead.
Keep it simple:
if (a.charAt(0)=='.') {
document.write(a.substr(1));
} else {
document.write(a);
}

Javascript replace function clarification

I am trying to learn javascript
i have this code:
x=x.replace(/^\s+|\s+$/g,"");
can i have a description about /^\s+|\s+$/g,""
searching to replace what with what ?
Regards
This is a regular expression. Basically \s matches whitespac characters and replaces them with "".
edit:
/ ... / marks the regex.
^\s+ Take 1or more whitespaces at the beginning of the string.
\s+$ Take 1 or more whitespaces at the end of the string.
/g Dont stop at the first match, but find all matches - "global flag"
It's removing whitespaces, either at the beginning or the end of the string, by replacing them with the empty string "".
You can find more about regular expressions here http://www.w3schools.com/jsref/jsref_obj_regexp.asp.
first, look at this: http://www.w3schools.com/jsref/jsref_replace.asp and then do not use stackoverflow for questions of this kind

How to remove square brackets in string using regex?

['abc','xyz'] – this string I want turn into abc,xyz using regex in javascript. I want to replace both open close square bracket & single quote with empty string ie "".
Use this regular expression to match square brackets or single quotes:
/[\[\]']+/g
Replace with the empty string.
console.log("['abc','xyz']".replace(/[\[\]']+/g,''));
str.replace(/[[\]]/g,'')
here you go
var str = "['abc',['def','ghi'],'jkl']";
//'[\'abc\',[\'def\',\'ghi\'],\'jkl\']'
str.replace(/[\[\]']/g,'' );
//'abc,def,ghi,jkl'
You probably don't even need string substitution for that. If your original string is JSON, try:
js> a="['abc','xyz']"
['abc','xyz']
js> eval(a).join(",")
abc,xyz
Be careful with eval, of course.
Just here to propose an alternative that I find more readable.
/\[|\]/g
JavaScript implementation:
let reg = /\[|\]/g
str.replace(reg,'')
As other people have shown, all you have to do is list the [ and ] characters, but because they are special characters you have to escape them with \.
I personally find the character group definition using [] to be confusing because it uses the same special character you're trying to replace.
Therefore using the | (OR) operator you can more easily distinguish the special characters in the regex from the literal characters being replaced.
This should work for you.
str.replace(/[[\]]/g, "");

JavaScript text manipulation

Using JavaScript and I want to replace any text between #anytext# with some text. I want to make it generic so I am thinking to make use of regular expression. How do I do it?
Example:replace('#hello#','Hi')
Try this:
str.replace(/#[^#]+#/g, 'Hi')
This will remove any sequences of # … # globally with Hi.
Edit    Some explanation:
/…/ is the regular expression literal syntax in JavaScript
#[^#]+# describes any sequence of a literal #, followed by one or more (+ quantifier) characters that is not a # (negated charcater class [^#]), followed by a literal #
the g flag in /…/g allows global matches; otherwise only the first match would be replaced
You can use the regex function of jquery to accomplish that...
So find the #'s with a regular expression and afterwards use the replace function with the text you want.
This has nothing to do with jQuery, but just plain old javascript.
var regexp = new RegExp("#([^#]+)#");
text.replace(re, "replacement text");
But what do you mean by generic? How generic do you want to make it?
You can find more information about regular expressions on http://regexp.info including how to use in in Javascript

Categories