I want to remove the new line character in a string, I tried to use something like:
myString.replace('\n', '');
But this doesn't work. What's the best way to do it?
A new string is returned, it is not modified in-place.
myString = myString.replace('\n', '');
If you have multiple newlines, you'll need to use a RegExp with the g flag set.
myString = myString.replace(new RegExp('\\n', 'g'), '');
And if you don't want to use RegExp you can simply use split and join:
myString = myString.split('\n').join('');
Related
I'm new to flex coding, for example if I have a string like "[123-456],[456-789]"
and I want to remove all the "[" and "]" which result in:
"123-456,456-789"
can string.replace() or trim() do the job for me?
tried several times still fail
In this case string.replace() should do the job!
Please see the documentation for String.replace()
var st: String = "[123-456],[456-789]";
var p1: RegExp = /\[|\]/g;
st = st.replace(p1, "");
trace(st) //"123-456,456-789"
What is the regular expression (in JavaScript if it matters) to only match if the text is an exact match? That is, there should be no extra characters at other end of the string.
For example, if I'm trying to match for abc, then 1abc1, 1abc, and abc1 would not match.
Use the start and end delimiters: ^abc$
It depends. You could
string.match(/^abc$/)
But that would not match the following string: 'the first 3 letters of the alphabet are abc. not abc123'
I think you would want to use \b (word boundaries):
var str = 'the first 3 letters of the alphabet are abc. not abc123';
var pat = /\b(abc)\b/g;
console.log(str.match(pat));
Live example: http://jsfiddle.net/uu5VJ/
If the former solution works for you, I would advise against using it.
That means you may have something like the following:
var strs = ['abc', 'abc1', 'abc2']
for (var i = 0; i < strs.length; i++) {
if (strs[i] == 'abc') {
//do something
}
else {
//do something else
}
}
While you could use
if (str[i].match(/^abc$/g)) {
//do something
}
It would be considerably more resource-intensive. For me, a general rule of thumb is for a simple string comparison use a conditional expression, for a more dynamic pattern use a regular expression.
More on JavaScript regexes: https://developer.mozilla.org/en/JavaScript/Guide/Regular_Expressions
"^" For the begining of the line "$" for the end of it. Eg.:
var re = /^abc$/;
Would match "abc" but not "1abc" or "abc1". You can learn more at https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Regular_Expressions
Is there any way to replace the new line character on free marker? I am trying this:
<#assign str = str?replace("(\r\n)+", "</p><p>")>
which worked in Java, but not in freemarker. How can I do this?
Ok, I found the problem. The replace function needs to know if the expresion is a regex, so I had to add 'r' as a parameter
<#assign str = str?replace("(\r\n)+", "</p><p>",'r')>
I have the following query attempting an update in CodeIgniter:
$sql = "UPDATE fanout.manual_data
SET call_leader_id = ?
WHERE id IN (?)";
$q = $this->db->query($sql, array($leaderID, implode(", ", $empIDs)));
The implode is creating a string of all the IDs in my array. However, that is resulting in the query looking like:
UPDATE fanout.manual_data SET call_leader_id = '55993' WHERE id IN ('57232, 0097726, 0076034');
When what I need is:
UPDATE fanout.manual_data SET call_leader_id = '55993' WHERE id IN (57232, 0097726, 0076034);
Only difference, is the single quotes surrounding the string of IDs. Is this something I need to do myself and skip over CI's query bindings (http://codeigniter.com/user_guide/database/queries.html) or is something CI can handle and I'm just missing a step?
Thanks.
I don't think you can skip that behavior. You're technically passing a string, so CI interprets it as such and simply surrounds it with quotes.
I think you're better off simply concatenating the $empIDs by hand (e.g. using a foreach loop), escaping them with $this->db->escape() in case you wanna be sure.
I have a XML string which needs to be displayed within HTML. I understand the first thing needed to be done here is to convert all '<' and '>' into '& lt;' and '& gt;' (ignore the space after & sign). This is what I am doing to replace '<' -
regExp = new RegExp("/</g");
xmlString = xmlString.replace(regExp, '& lt;');
xmlString does not change.
Also, trace(regExp.test("<")); prints false.
What is wrong here?
replace returns a new string, it doesn't modify the old one. So if you want to overwrite the old you have to do the following:
xmlString = xmlString.replace(regExp, '<');
Or if you don't want to overwrite the old one, just store the result in a new variable.
var newString = xmlString.replace(regExp, '<');
The issue is the way you create your RegExp object.
Because your using the RegExp constructor, don't include the / characters:
regExp = new RegExp("<", "g");
or use / as a shortcut:
regExp = /</g;
See this page for more details: http://livedocs.adobe.com/flash/9.0/ActionScriptLangRefV3/RegExp.html