javascript - .replace() is working on one string but not another - javascript

Pulling data with AJAX, into an array, everuthing there works fine, then I have this...
$.each(data, function (key, value){
var add = value[5]+value[6];
var sub = add.replace(" ","");
var link = 'http://'+sub+'.mydomain.com';
}
//OUTPUT: http://RR1 Box 22USHIGHWAY 67.NextHomeTown.com
This isn't working. It's not replacing any space characters.
Now, here's where it gets fun. This works on every other DB entry that is returned that has a space. Crazy, right?
Is there some type of character encoding that might be causing it not to recognize the space character that is used in this particular entry? The MySQL table has them entered as varchar, but at this point in the process, they're both just text strings right? So it shouldn't matter.

This will only replace the first spacebar it will match. Use this to replace all spacebars:
var sub = add.replace(/\s/g,"");

Since you report the desired behaviour with other tables, it's perhaps not relevant - but don't forget that in javascript, the string replace function only replaces the first instance of the searchString, unless you use a regular expression.
"red, red, red".replace(/ /g, "");
"red,red,red"
"red, red, red".replace(" ", "");
"red,red, red"

Related

Django: Add space after a comma in a string using javascript in a template

In an html template I get a variable's value from a select drop down list like
one or more,two
In need to add an extra space after the comma and I need my string to be
one or more, two
My current javascript is:
$('#id_diag-diagnosis_option').change(function () {
var value = $(this).val()
alert(value)
Do you have any idea?
value.replace(/\s/g, '').split(",").join(", ")
should work for all cases (anything other than one space after comma)
var after = before.replace(",", ", ");
Should work (Before being the string before the change, and after being after)

How to read only part of a string in Javascript

I am pulling in a string from another web page. I want to read that string into a variable but only after a certain point. Eg:
#stringexample
var variable;
I want variable to equal stringexample but not contain the # how could I do this?
This is how I am using the variable at the moment.
$("#Outputajax").load("folder/"+ variable +".html");
This is the way that works but isn't a variable.
$("#Outputajax").load("folder/webpage.html");
If you just want to trim of the first character, then you can use substring...
var input = "#stringexample";
input = input.substring(1);
//input = "stringexample"
Here is a working example
var myVariable = stringExample.replace('#','');
Could just use variable.substr(1) to cut off the first character.
If you want to specifically remove the hash from the start (but do nothing if the hash isn't there), try variable.replace(/^#/,"")
I understand you want to get everything in the string AFTER the hashtag. The other solutions will leave anything ahead of the hashtag in as well. And substring does not work if the hashtag is not the first symbol.
variable= "#stringexample".split("#")[1];
This splits the string into an array of strings, with the parameter as the point where to split, without including the parameter itself. There will be an empty string as the first parameter, and everything after the hashtag is the second string.
var slicer = function(somestring){
var parsedString = somestring;
parsedString = parsedString.slice(1);
return parsedString
}
// run from yors function with some string
var someYouVar = slicer("#something")

Parsing Text with jQuery

I'm attempting to parse a text string with jQuery and to make a variable out of it. The string is below:
Publications Deadlines: armadllo
I'm trying to just get everything past "Publications Deadlines: ", so it includes whatever the name is, regardless of how long or how many words it is.
I'm getting the text via a the jQuery .text() function like so:
$('.label_im_getting').text()
I feel like this may be a simple solution that I just can't put together. Traditional JS is fine as well if it's more efficient than JQ!
Try this,
Live Demo
First part
str = $.trim($('.label_im_getting').text().split(':')[0]);
Second part
str = $.trim($('.label_im_getting').text().split(':')[1]);
var string = input.split(':') // splits in two halfs based on the position of ':'
string = input[1] // take the second half
string = string.replace(/ /g, ''); // removes all the spaces.

javascript beginner- how to obtain text occurring after/before a string value, in a string variable

var mainvalue="This is first sentence. This is 2nd sentence. This is 3rd sentence.";
var matchwith="This is first";
Now I want to obtain the text in variable 'mainvalue' that occurs after the value stored in variable matchwith.... It is assumed here that the value in 'matchwith' is present in mainvalue.
Another question that I have...What if the value in 'matchwith' is stored more than once in mainvalue?
i.e.
var mainvalue="This is first sentence. This is 2nd. Repeat of This is first sentence. Yet another sentence";
Now, I want to obtain the text in 'mainvalue' that occurs before the last occurence of 'This is first sentence'.
Also, I want to obtain the text in 'mainvalue' that occurs after the first occurence of 'This is first sentence'
How do I do this?
Finally, how do I determine of the value "This is first sentence" occurs at beginning of mainvalue variable or not? Again, how do I find out if this value occurs right at the end of mainvalue variable or not?
I prefer pure JS, but even use of libraries like Jquery is ok with me...
First get the position of the substring in the haystack:
var pos = mainvalue.indexOf(matchwith);
Then you can get the stuff before it:
var before = mainvalue.substr(0,pos);
Or after:
var after = mainvalue.substr(pos+matchwith.length);
Simple. Now, on to duplicates.
In the case you outlined, you can get the string after the first match, and then get the part of that string that is before the second match (since that one will become the first). More flexible, though, is this:
var pieces = mainvalue.split(matchwith);
Now you can get the index of the piece you like.
To see if the string starts or ends with your search pattern, you could see if the "string before/after the match" is empty, respectively.
There is many ways to do that,
if have no time to think use regexp replace,
example:
var mainvalue="This is first sentence. This is 2nd sentence. This is 3rd sentence.";
var matchwith="This is first";
var out=mainvalue.replace(new RegExp(matchwith),'')

Remove part an ever-changing text string with javascript?

I have a string of text "AB-123-2011-07-09", and need to remove everything except "123", then add a "#" sign to the end result.
The string "123" is ever increasing in number, as is the "2011-07-09" (a date). Only "AB" stays the same.
So the end result would be: #123
Is this possible?
Thanks.
EDIT: Just to clarify, I was needing a script that could globally search a page and replace any text which had the format of "AB-xxx-xxxx-xx-xx" with just the digits highlighted here in bold, then adding the "#" before it.
Currently there are only 3 digits in that position, but in the future there may be four.
My code:
function Replace() {
var OldString = "AB-123-2011-07-09";
var NewString = OldString.replace(/^AB-(\d+)-.*/, "#$1");
document.body.innerHTML = document.body.innerHTML.replace(OldString, NewString);
}
window.onload = Replace();
So far it only replaces 1 instance of the string, and uses a fixed string ("AB-123-2011-07-09").
What regular expression do I need to make the 'OldString' dynamic, rather than it being fixed as it is now?
var data = "AB-123-2011-07-09";
var field = data.split('-')[1];
document.write("#" + field);
http://jsfiddle.net/efortis/8acDr/
The following regex would work, but in this case I don't think you need a regex at all (as #Eric has already shown).
"AB-123-2011-07-09".replace(/^AB-(\d+)-.*/, "#$1");
This results in the value #123
http://jsfiddle.net/3XhbE/
Does this work?
var result = mystring.replace(new RegExp(AB-([0-9]+)-[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9], "g"),"#$1");
mystring is the "AB-123-2011-07-09" string and result would be "#123".
This is of course possible. This regex would do the trick:
“AB-123-2011-07-09“.replace(/^AB-(\d+)-\d+-\d+-\d+$/, “#$1“);
It also checks you given syntax and that there is nothing else in the string.
migg

Categories