0

When I want to replace "abc" by "xyz" i use the following command:

str.split("abc").join("xyz");

But what if I have the following string "This is just a test string" and would like to replace only the second whitespace by "\n".

2
  • If i understands this correctly you want the resulting String look like this: "This is\njust a string"? Commented Feb 10, 2013 at 18:57
  • @Johan: yes. I want the second whitespace to be replaced by "\n". Commented Feb 10, 2013 at 19:04

2 Answers 2

2

To improve on @fsbmain's answer, n-th occurence can be replaced in using '/(( [^ ]+){3}) /' (http://regexr.com?33nm5):

var str:String = ("This is just a test string").replace(/(( [^ ]+){3}) /, "$1\n");
trace(str);

The output would be:

This is just a
test string
Sign up to request clarification or add additional context in comments.

Comments

0

You can use this regExp:

    var str:String = ("This is just a test string").replace(/(.[^ ]+ .[^ ]+) /, "$1\n");
    trace(str);

output:

This is
just a test string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.