Trimming String in Javascript - javascript

I have string like var a=""abcd""efgh"".How do I print the output as abcd""efgh by removing first and last double quote of a string I used a.replace(/["]/g,'') but it is removing all the double quotes of a string.How do i get the output as abcd""efgh.Suggest me an idea.

You can use
var a='"abcd""efgh"';
a.replace(/^"+|"+$/g, '');
From the comments here is the explanation
Explanation
There are 2 parts ^"+ and "+$ separated by | which is the regex equivalent of the or
^ is for starts-with and "+ is for one or more "
Similarly $ is for ends-with
The //g is for global replacement otherwise the only the first occurrence will be replaced

Try use this
var a='"abcd""efgh"';
a.replace(/^"|"$/g, '');

Related

How to get next 3 characters after a substring if it exist inside a string?

I have this string that's. The &# substring is common but number after it changes in almost every object of my JSON data. So I want to detect if there is this substring, then get next three characters after it replace it with something else. How can I do it?
You can do it like this
var para = 'that's';
para = para.substr(para.indexOf('#')+1, 3);
syntax:
substr(start, length)
indexOf(searchvalue, [start])
Assuming you want to replace everything from &# until ; (if not, please update your question by specifying expected output):
You can use String.prototype.replace() with a regular expression:
var para = 'some string ' middle † end';
para = para.replace(/&#([\d]*);/g, 'replacement');
The g modifier is important to replace all occurences in the string.
With the RegEx used, you can include the found number (between &# and ;) in the replacement string by using $1.
you can define and use a utility function to replace HTML entities like that:
function decode(text, replaceWith = '') {
return text.replace(/&#(\d+);/g, replaceWith)
}

Easiest way to replace some single quotes in javascript?

So I have the following example string: 'Sam's Place' and I want to change it to "Sam's Place". Is there an easy way to manipulate a sting with single quotes into using double quotes? The important part is to keep the one single quote in the string.
I have tried:
var samsPlaceName = samsPlace.replace(/'/g, '"');
// And:
JSON.stringify(samsPlace);
// Both give me:
'Sam"s Place'
'"Sam's Place"'
All I want is to change the string to be: "Sam's Place".
Can this be done?
// assume you have a string
var samsPlace = "'Sam's Place'";
console.log(samsPlace);
// replace the single quote at the beginning and end of string with double quote by
// using anchors where ^ denotes BOS (beginning of string) and $ denotes EOS (end of
// string)
console.log(samsPlace.replace(/^'|'$/g, '"'));
Can't you use this?
var str = '"Sams place"'
while(str.indexOf('"') != -1){
str = str.replace('"',"'");
}
I hope i don't get you wrong :S

How to replace the just end of a string

I want a regex that replaces the end of a string if it ends in '.id'. E.g if I have a string called 'password' nothing gets replaced. If I have another string called 'idiot', nothing gets replaced. But, if I have 'email.id' it gets replaced with an empty string.
I made using of loadash's trimEnd but I noticed it was trimming the letter d in password and the result was passwor.
Search with \.id$ and replace with ''. Regex101 Demo
simply do with pattern /\.id$/g using regex
Explanation:
\.id is match the .id in the string
$ is match the position at end of the string
Demo Regex
console.log('password'.replace(/\.id$/g,""))
console.log('email.id'.replace(/\.id$/g,""))
If the ".id" is static you can use
var newstr = str.split('.id').join('');
If it is dynamic you can use
var newstr = str.substr(0, str.indexOf('.'));

java script Regular Expressions patterns problem

My problem start with like-
var str='0|31|2|03|.....|4|2007'
str=str.replace(/[^|]\d*[^|]/,'5');
so the output becomes like:"0|5|2|03|....|4|2007" so it replaces 31->5
But this doesn't work for replacing other segments when i change code like this:
str=str.replace(/[^|]{2}\d*[^|]/,'6');
doesn't change 2->6.
What actually i am missing here.Any help?
I think a regular expression is a bad solution for that problem. I'd rather do something like this:
var str = '0|31|2|03|4|2007';
var segments = str.split("|");
segments[1] = "35";
segments[2] = "123";
Can't think of a good way to solve this with a regexp.
Here is a specific regex solution which replaces the number following the first | pipe symbol with the number 5:
var re = /^((?:\d+\|){1})\d+/;
return text.replace(re, '$15');
If you want to replace the digits following the third |, simply change the {1} portion of the regex to {3}
Here is a generalized function that will replace any given number slot (zero-based index), with a specified new number:
function replaceNthNumber(text, n, newnum) {
var re = new RegExp("^((?:\\d+\\|){"+ n +'})\\d+');
return text.replace(re, '$1'+ newnum);
}
Firstly, you don't have to escape | in the character set, because it doesn't have any special meaning in character sets.
Secondly, you don't put quantifiers in character sets.
And finally, to create a global matching expression, you have to use the g flag.
[^\|] means anything but a '|', so in your case it only matches a digit. So it will only match anything with 2 or more digits.
Second you should put the {2} outside of the []-brackets
I'm not sure what you want to achieve here.

new RegExp. test

I have posted a problem in the above link - regExpression.test.
Based on that I have done like bellow that also produces an error.
var regExpression=new RegExp("^([a-zA-Z0-9_\-\.]+)$");
alert (regExpression.test("11aa"));
You need to escape your \ since you're declaring it with a string, like this:
var regExpression=new RegExp("^([a-zA-Z0-9_\\-\\.]+)$");
^ ^ add these
You can test it here.
You can also use the literal RegExp syntax /…/:
var regExpression = /^([a-zA-Z0-9_\-\.]+)$/;
By the way: The . does not need to be escaped in character classes anyway. And if you put the range operator at the begin or the end of the character class or immediately after a character range, it doesn’t need to be escaped either:
var regExpression = /^([a-zA-Z0-9_.-]+)$/;

Categories

Resources