Use a capture group and add it to the replace value with $1
var p = new RegExp('^((\\S+ ){1})(\\S+)');
$("#sentence").html(function(k,v) {return v.replace(p, "$1was");});
Full example:
<!DOCTYPE html>
<html>
<head>
<script
src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js">
</script>
</head>
<body>
<div id="sentence">This is some sentence that has the word is.</div>
<script>
var p = new RegExp('^((\\S+ ){1})(\\S+)');
$("#sentence").html(function(k,v) {return v.replace(p, "$1was");});
</script>
</body>
</html>
Explanation:
^ Matches the beginning of the string.
(\\S+ ){n} Repeats n times, but this would also repeat the capture. (Keeping only the last group captured.)
((\\S+ ){1}) Embed the repeat inside a capture group. $1
(\\S+ ){1} This is now the second capture group. $2 Only the last iteration is stored in $2
(\\S+) Capture the extra word to be replaced. $3
v.replace(p, "$1was"); $1 adds the first capture group and back into the string. The second capture group is left out.
is(looking for a word boundary before it, so that it doesn’t replace “this”), without caring about that it happens to be the second word?"This is some sentence that has the word is.".replace(/\bis/, "was")