This question already has answers here:
How do I create a new line in Javascript?
(18 answers)
Closed 5 years ago.
To add a new line into a JavaScript string can I use the following code?
Is it according to programming standards? Is there another way to do this?
For example,
var string = "check";
alert(string);
var newstring = string + "\n\n";
alert(newstring);
I think it is fine. If you are using alert for the debugging purposes, Please use
console.log("string + "\n"); instead.
\n
is the right way to handle new lines in alert boxes.
Simply add a \n everytime you want a new line in your string :
var str = 'Test\nTest2\nTest3';
alert(str);
Related
This question already has answers here:
How to split newline
(13 answers)
Closed 7 years ago.
We are Developing phonegap application.We get Data form CSV file. It's look like this
We need Data Split into two strings Like
String1
String2
We tried like this but We don't have luck so Please guide me
var split = string.split('\r\n');
Please help me
try this:
var split = string.split(/\n/);
Replace the new line characters with space and then split the string with space
string.replace( /\n/g, " " ).split(" ");
UPDATE:
var string1=string.substring(0,string.indexOf("TOTALAMOUNT"));
var string2=string.substring(string.indexOf("TOTALAMOUNT"),string.length);
Or if your string contains \n then:
var string1=string.substring(0,string.indexOf("\n"));
var string2=string.substring(string.indexOf("\n"),string.length);
alert(string1);
alert(string
Fiddle
This question already has answers here:
Create RegExps on the fly using string variables
(6 answers)
Closed 8 years ago.
I need to use data attribute for regex just as
<div data-regex="REGEX-HERE">
and then get the value by javascript and put in a variable. and then do a test like
var regex = $(this).attr("data-regex");
regex.test(name)
when I tried to use "^[\x20-\x7E]+$" for testing english character is didn't work.
Note when I tried this
var regex = /^[\x20-\x7E]+$/;
It worked.
Thanks in advance
You can do this:
var regex = new RegExp("^[\x20-\x7E]+$",""); // Modifiers on the tend
So finally:
var regex = new RegExp($(this).data("regex"));
regex.test(name)
This question already has answers here:
Replace method doesn't work
(4 answers)
Closed 8 years ago.
Can anyone tell me how to replace "{ with { using JavaScript?
Here is what I am trying to do:
string.replace(/\"\{/g, "{");
Your regex is fine. Don't forget that strings are immutable in javascript. The replace function doesn't change the receiver string but builds a new one.
So you must do
string = string.replace(/\"\{/g, "{");
In case you were using this directly on string you should use it on an instance of string. Not on string type.
(I know it sounds too trivial, but otherwise this code should have worked. :) )
var stringTypeVariable = 'some string "{ with target pattern';
var replacedVariable = stringTypeVariable.replace(/\"\{/g, "{");
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How can I match multiple occurrences with a regex in JavaScript similar to PHP’s preg_match_all()?
I am trying to parse an xml document like this:
var str = data.match("<string>" + "(.*?)" + "</string>");
console.log(str);
I want to get all the elements between the [string] in an array but for some reason, it only returns the first string element found. Im not good with regular expressions so Im thinking this is just a small regex issue.
You want it to be global g
var str="<string>1</string><string>2</string><string>3</string>";
var n=str.match(/<string>(.*?)<\/string>/g);
//1,2,3
You have to form the RegEx adding a g to it like
/Regex/g
This question already has answers here:
how to pass a variable into an regular expression in javascript [duplicate]
(3 answers)
Closed 8 years ago.
I have a string of text that has the value of the selected text. Id like to do a replace() of this text in a given div of text.
so I know I can do that with the following:
myelement.replace(/foo/g, 'bar');
However I need to do it with my string ie:
myelement.replace(/*mystring*/g, 'bar');
So I tried:
mystring = '/'+mystring+'/g';
myelement.replace(mystring, 'bar');
Which didnt work, so i tried (which i knew wouldn't work):
myelement.replace(/+mystring+/g, 'bar');
So how can i do this?
I've coded something up for you guys in jsfiddle --> HELP ME PLEASE!
http://jsfiddle.net/2rG2V/
The change being to use new RegExp(st, "g") instead of creating the string as you did before. /test/g is just a shortcut way of creating a RegExp object.
simply replace your
regSt = '/'+st+'/g';
with
regSt = new RegExp(st,'g');
more info on regExp http://www.w3schools.com/jsref/jsref_obj_regexp.asp
eval('myelement.replace(/' + mystring + '/g, "bar");');