Javascript String replace "'" - javascript

How do I replace ' in javascript. For example I want to convert O'conor to O-conor.
This doesnt work. I am doing something stupid.
var dummyStr = "O'conor";
dummyStr.replace("'","-");
console.log(dummyStr); //prints O'conor
dummyStr.replace(/'/g,"-"); //still prints O'conor not O-conor
Please mark duplicate if this has already been asked elsewhere.

replace (cf. replace on W3Schools) does not modify the current string. You have to assign it like this :
dummyStr = dummyStr.replace("'","-");
console.log(dummyStr); //prints O-conor

You need to assign a variable to the return value of replace()
e.g.
var dummyStr = "O'conor";
var ammendedString = dummyStr.replace("'","-");
console.log(ammendedString );

you just need to store this to some variable after replace, like below
dummyStr = dummyStr.replace("'","-");

dummyStr = dummyStr.replace("'","-");
Btw for replacing all:
Replace All - StackOverFlow

Related

Javascript regexp replace capturing

I'm trying to use regex in a Nodejs app. I usually use it in Python and it seems to have some differences.
Here is the problem :
I have this string \newcommand{\hello}{#replace} and I want to replace #replace by REPLACED in the second curly bracelets ONLY when I found \hello in the first curly bracelets. So the expected result is : \newcommand{\hello}{REPLACED}
I try this:
r = new RegExp('\\newcommand{\\hello}{(.*?)}');
s = '\\newcommand{\\hello}{#replace}';
s.replace(r, 'REPLACED');
But nothing is replaced... any clue?
r = new RegExp(/\\newcommand{\\hello}{#replace}/);
s = '\\newcommand{\\hello}{#replace}';
let a = s.replace(r, '\\newcommand{\\hello}{REPLACED}');
console.log(a)
Output would be : "\newcommand{\hello}{REPLACED}"
I'm not sure if I understood the question correctly. Is this what you're looking for?
function replaceWith(myReplacement) {
var original = "\\newcommand{\\hello}{#replace}";
var regex = "{\\hello}{#replace}";
return original.replace(regex, `{\\hello}{${myReplacement}}`)
};
console.log(replaceWith("World"));
You don't need regex at all to perform this kind of operation. You can simply use string at first parameter:
s = '\\newcommand{\\hello}{#replace}';
s.replace('#replace', 'REPLACED'); // => "\newcommand{\hello}{REPLACED}"

Regex append characters to a substring

My string comes in two flavours-
var a = /aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo
or
var a = /aid/f82eb514073124cd10d468b74eee5663#/propertyinfo
I want to append the content that comes after aid/ and before ? or # with "-test". In either of the above scenarios the result would be f82eb514073124cd10d468b74eee5663-test
hence
a = /aid/f82eb514073124cd10d468b74eee5663-test#/propertyinfo
or
a = = /aid/f82eb514073124cd10d468b74eee5663-test?sg=1#/propertyinfo
Seems like you're looking for something like this.
Regular expression /\/aid\/[0-9A-F]*/i and replacement expression $0-test.
JavaScript is a little bit different than just plain regular expression antics, so here you go;
var a = "/aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo";
alert(a.replace(/(\/aid\/[0-9A-F]*)/i, "$1-test"));
given your examples I guess that string after /aid/ is some kind of md5 hash
this should work for you:
'/aid/f82eb514073124cd10d468b74eee5663#/propertyinfo'.replace(new RegExp('/aid/([a-f0-9]{32})'), '$1-test');
if you don't want to be that much specific about length, you can try the following:
'/aid/f82eb514073124cd10d468b74eee5663#/propertyinfo'.replace(new RegExp('/aid/([a-f0-9]+)'), '$1-test');
Simple solution using String.replace function:
var a = '/aid/f82eb514073124cd10d468b74eee5663sg=1#/propertyinfo',
result = a.replace(/aid\/([^?#]+)(?=\?|#)/, "aid/$1-test");
console.log(result); // /aid/f82eb514073124cd10d468b74eee5663-test?sg=1#/propertyinfo
I suggest replacing directly the # or ? so the regex is nice and simple. :)
var a = "/aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo";
var b = "/aid/f82eb514073124cd10d468b74eee5663#/propertyinfo";
console.log(a.replace(/([\?#])/,"-test$1"));
console.log(b.replace(/([\?#])/,"-test$1"));
var a = '/aid/f82eb514073124cd10d468b74eee5663?sg=1#/propertyinfo';
a.replace(/(\/aid\/.+)(\?sg=1)(#\/propertyinfo)/,function(text,c,d,e){
return c+'-test'+e;
})
//Output: "/aid/f82eb514073124cd10d468b74eee5663-test#/propertyinfo"
a.replace(/(\/aid\/.+)(\?sg=1#\/propertyinfo)/,function(text,c,d){
return c+'-test'+d;
});
//Output: "/aid/f82eb514073124cd10d468b74eee5663-test?sg=1#/propertyinfo"

How to get part of string using javascript?

I have an array with one element
ClickMeArray[0] = "ClickMe=a-6,a-7,a-8,a-9,"
I want variable to return elements after ClickMe= from ClickMeArray.
Output should be a-6,a-7,a-8,a-9,.
ClickMeArray[0].split('ClickMe=')[1];
Try This
var val = "ClickMe=a-6,a-7,a-8,a-9,";
var myString = val.substr(val.indexOf("=") + 1);
There are many way to do this work. One way is using .replace(). You can replace ClickMe= with empty to getting another part of string.
var ClickMeArray = "ClickMe=a-6,a-7,a-8,a-9,";
console.log(ClickMeArray.replace('ClickMe=',''));

jQuery - trim the variable

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

selecting second string point using js substring

i want to select a sting from the long para. it has number of dot('.')s. i want to trim the word from the second one, is it any way to do this?
example
var name = "one.two.three";
name.substring(0,name.indexOf('.'))
name.substring(0,name.lastIndexOf('.'))
from above trimming in case if i use indexOf it gives first word (one), if i use lastIndex of it gives the word (three), but i need to select the second one, to get value as 'second'
how can i trim this using indexOf method? or to select multicombination strings like one.three or one.two, or two.three?
thanks in advance!
use string.split.
e.g.
name.split(".")[1]
var name="one.two.three";
var result=name.split(".").slice(0,2).join(".");
Example:
"".split(".").slice(0,2).join(".") // return ""
"one".split(".").slice(0,2).join(".") // return "one"
"one.two".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three".split(".").slice(0,2).join(".") // return "one.two"
"one.two.three.four.five".split(".").slice(0,2).join(".") // return "one.two"
Is that work for you ?
var name = "one.two.three";
var params = name.split('.');
console.log(params[1]);
use Split
var name = "one.two.three";
var output = name.split('.');
alert(output[1]);
example here

Categories

Resources