in vb6 there was a very handy function for string manipulation which could put a character at a certain position of another string and i'm looking for an extended jquery equivalent.
let's say i'm having this string:
var mystring = "__1__";
when applying the function:
var mystring = mid(mystring,4,"x");
it should return __1x_
another example:
var mystring = "";
var mystring = mid(mystring,5,"x");
should return: ____5
i know it requires string manipulation using substr but i was wondering if there's a more elegant way?
thanks
This can be simulated in several ways although there is no such specific function (splice is standard only on Arrays, not Strings).
The easiest one-expression way I know of is with a String.replace when adding to a location "past the end of the string" is not required. Of course String.slice is also a perfectly valid approach, and may be arguably easier to understand.
mystring = "__1__"
// where 3 represents the "characters to skip before inserting"
// and 1 represents the "number of characters to replace"
midstr = mystring.replace(/([^]{3})[^]{0,1}/, "$1x")
Neither the above nor a basic slice will work like the 2nd example without additional prepend-as-needed logic.
Related
I have problem one of my string has repeated url inside and I want to remove it. What's the best way to do it in javascript?
Following is example of string I referring to.
var str = "http://www.example.comhttp://www.example.com"
One solution is to reassign str to half of itself.
str = str.substring(str.length/2)
(This assumes the string will always follow the same format as the example you gave.)
I am trying to replace a string with two fractions "x={2x-21}/{x+12}+{x+3}/{x-5}" with a string
"x=\frac{2x-21}{x+12}+\frac{x+3}{x-5}" (i.e. convert from jqMath to LaTex).
To achieve this, I've written the following code:
var initValue = "(\{.*\}(?=\/))\/(\{.*\})";
var newValue = "\\frac$1$2";
var re = new RegExp (initValue,"g");
var resultString = givenString.replace(re,newValue);
return resultString;
This code seems to work for strings with just one fraction (e.g. "x={2x-21}/{x+12}") but when I try to apply it to the example with two fractions, the result is x=\frac{2x-21}/{x+12}+{x+3}{x-5}. As far as I understand, regex engine captures {2x-21}/{x+12}+{x+3} as the first group and {x-5} as the second group. Is there any way to get the result that I want with regular expressions?
The same question is applicable to other patterns with several non-nested delimiters, for example: "I like coffee (except latte) and tea (including mint tea)". Is it possible to capture both statements in parentheses?
If not, I will probably have to write a function for this (which is ok, but I wanted to make sure this is the right approach).
({[^}]+})\/({[^}]+})
Try this.See demo.Repalce by \\frac$1$2.
http://regex101.com/r/tF5fT5/24
There is a part in my string from, to which I would like to replace to an another string replace_string. My code should work, but what if there is an another part like the returned substring?
var from=10, to=17;
//...
str = str.replace(str.substring(from, to), replace_string);
For example:
from=4,to=6
str = "abceabxy"
replace_string = "zz"
the str should be "abcezzxy"
What you want to do is simple! Cut out and replace the string. Here is the basic tool, you need scissor and glue! Oops I mean string.Split() and string.Replace().
How to use?
Well I am not sure if you want to use string.Split() but you have used string.Replace() so here goes.
String.Replace uses two parameters, like this ("one", "two") what you need to make sure is that you are not replacing a char with a string or a string with a char. They are used as:
var str="Visit Microsoft!";
var n=str.replace("Microsoft","W3Schools");
Your code:
var from=10, to=17;
//...
var stringGot = str.replace(str.substring(from, to), replace_string);
What you should do will be to split the code first, and then replace the second a letter! As you want one in your example. Thats one way!
First, split the string! And then replaced the second a letter with z.
For String.Replace refer this: http://www.w3schools.com/jsref/jsref_replace.asp
For String.SubString: http://www.w3schools.com/jsref/jsref_substring.asp
For String.Split: http://www.w3schools.com/jsref/jsref_split.asp
Strings are immutable. This means they do not change after they are first instantiated. Every method to manipulate a string actually returns a new instance of a string. So you have to assign your result back to the variable like this:
str = str.replace(str.substring(from, to), replace_string);
Update: However, the more efficient way of doing this in the first place would be the following. it is also less prone to errors:
str = str.substring(0, from) + replace_string + str.substring(to);
See this fiddle: http://jsfiddle.net/cFtKL/
It runs both of the commands through a loop 100,000 times. The first takes about 75ms whereas the latter takes 20ms.
I have this string:
var str = "jquery12325365345423545423im-a-very-good-string";
What I would like to do, is removing the part 'jquery12325365345423545423' from the above string.
The output should be:
var str = 'im-a-very-good-string';
How can I remove that part of the string using php? Are there any functions in php to remove a specified part of a string?
sorry for not including the part i have done
I am looking for solution in js or jquery
so far i have tried
var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("jquery12325365345423545423","");
but problem is numbers are randomly generated and changed every time.
so is there other ways to solve this using jquery or JS
The simplest solution is to do it with:
str = str.replace(/jquery\d+/, '').replace(' ', '');
You can use string replace.
var str = "jquery12325365345423545423im-a-very-good-string";
str.replace('jquery12325365345423545423','');
Then to removespaces you can add this.
str.replace(' ','');
I think it will be best to describe the methods usually used with this kind of problems and let you decide what to use (how the string changes is rather unclear).
METHOD 1: Regular expression
You can search for a regular expression and replace the part of the string that matches the regular expression. This can be achieved through the JavaScript Replace() method.
In your case you could use following Regular expression: /jquery\d+/g (all strings that begin with jquery and continue with numbers, f.e. jquery12325365345423545423 or jquery0)
As code:
var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("/jquery\d+/g","");
See the jsFiddle example
METHOD 2: Substring
If your code will always have the same length and be at the same position, you should probably be using the JavaScript substring() method.
As code:
var str="jquery12325365345423545423im-a-very-good-string";
var code = str.substring(0,26);
str=str.substring(26);
See the jsFiddle example
Run this sample in chrome dev tools
var str="jquery12325365345423545423im-a-very-good-string";
str=str.replace("jquery12325365345423545423","");
console.log(str)
I have a string from which I am trying to get a specif value. The value is buried in the middle of the string. For example, the string looks like this:
Content1Save
The value I want to extract is "1";
Currently, I use the built-in substring function to get to remove the left part of the string, like this:
MyString = "Content1Save";
Position = MyString;
Position = Position.substring(7);
alert(Position); // alerts "1Save"
I need to get rid of the "Save" part and be left with the 1;
How do I do that?
+++++++++++++++++++++++++++++++++++++++++
ANSWER
Position = Position.substr(7, 1);
QUESTION
What's the difference between these two?
Position = Position.substr(7, 1);
Position = Position.substring(7, 1);
You can use the substr[MDN] method. The following example gets the 1 character long substring starting at index 7.
Position = Position.substr(7, 1);
Or, you can use a regex.
Position = /\d+/.exec(Position)[0];
I would suggest looking into regex, and groups.
Regex is built essentially exactly for this purpose and is built in to javascript.
Regex for something like Content1Save would look like this:
rg = /^[A-Za-z]*([0-9]+)[A-Za-z]*$/
Then you can extract the group using:
match = rg.exec('Content1Save');
alert(match[1]);
More on regex can be found here: http://en.wikipedia.org/wiki/Regular_expression
It highly depends on the rules you have for that middle part. If it's just a character, you can use Position = Position.substring(0, 1). If you're trying to get the number, as long as you have removed the letters before it, you can use parseInt.
alert(parseInt("123abc")); //123
alert(parseInt("foo123bar")); //NaN
If you're actually trying to search, you'll more often than not need to use something called Regular Expressions. They're the best search syntax JavaScript avails.
var matches = Position.match(/\d+/)
alert(matches[0])
Otherwise you can use a series of substr's, but that implies you know what is in the string to begin with:
MyString.substr(MyString.indexOf(1), 1);
But that is a tad annoying.